自定义获取广播权限
Intent intent= new Intent();
intent.setAction("Help_stitch");
intent.setComponent(new ComponentName(getPackageName(),"com.examplehq.forhelp.MyReceiver"));
sendBroadcast(intent);
定义有序广播
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter2 = new IntentFilter();
filter2.addAction("intercept_stitch");
filter2.setPriority(999);
this.registerReceiver(new MyReceiverOne(), filter2);
IntentFilter filter1 = new IntentFilter();
filter1.addAction("intercept_stitch");
filter1.setPriority(1000);
this.registerReceiver(new MyReceiverTwo(), filter1);
IntentFilter filter3 = new IntentFilter();
filter3.addAction("intercept_stitch");
filter3.setPriority(998);
this.registerReceiver(new MyReceiverThree(), filter3);
}
public void click(View view) {
Intent intent =new Intent();
intent.setAction("intercept_stitch");
sendBroadcast(intent,null);
}
}
public class MyReceiverOne extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyReceiverOne","第一个自定义广播接收者");
Toast.makeText(context, "第一个自定义广播接收者", Toast.LENGTH_SHORT).show();
}
}