发送自定义广播
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v) {
//发送自定义广播
Intent intent = new Intent();
//自定义action
intent.setAction("yhbbroadcast1");
sendBroadcast(intent);
}
}
接收自定义广播
- 只需要接收的action:name与自定义的一致就可以
<receiver
android:name=".CustomReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="yhbbroadcast1"></action>
</intent-filter>
</receiver>
网友评论