本文实例为大家分享了Android BroadcastReceiver广播使用的具体代码,供大家参考,具体内容如下
静态的BroadcastReceiver
主要代码
public class MyReceiver extends BroadcastReceiver { @Override //接受广播时回调 public void onReceive(Context context, Intent intent) { //接收广播 if(intent != null){ //接收到是什么广播 String action = intent.getAction(); Log.e("测试",action); } } }
在AndroidManifest.xml里设置权限
动态的BroadcastReceiver
主要代码
1.设置一个Java类继承BroadcastReceiver
public class MyReceiverD extends BroadcastReceiver { @Override //接受广播时回调(不能做耗时操作,必须开子线程) public void onReceive(Context context, Intent intent) { //接收广播 if(intent != null){ //接收到是什么广播 String action = intent.getAction(); Log.e("测试",action); } } }
在AndroidManifest.xml里设置权限
//因为是动态设置就不需要在里面设置别的了
3.MainActivity
//新建一个广播接收器 动态广播 receiverD = new MyReceiverD(); //接收那种广播 IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); intentFilter.addDataScheme("package"); intentFilter.addAction(Intent.ACTION_BATTERY_LOW); //注册广播接收器 registerReceiver(receiverD,intentFilter); protected void onDestroy() { super.onDestroy(); //取消注册关闭接收器 if (receiverD != null){ unregisterReceiver(receiverD); } }
随便卸载一个应用控制台就会显示
自定义的BroadcastReceiver
1.还是准备一个Java继承BroadcastReceiver
public class MyReceiverD_zdy extends BroadcastReceiver { private TextView txt; public MyReceiverD_zdy(TextView txt) { this.txt = txt; } public MyReceiverD_zdy() { } @Override public void onReceive(Context context, Intent intent) { //接收广播 if(intent != null){ //接收到是什么广播 String action = intent.getAction(); Log.e("测试",action); //判断是什么广播,是否是自己自定义的广播 if (TextUtils.equals(action,MainActivity.MY_ACTION)){ //获取广播携带的数据 String cOntent= intent.getStringExtra(MainActivity.BROADCAST_CONTENT); if (txt != null){ txt.setText("接收到的action是:"+action+"\n接收到的内容是"+content); } } } } }
2.activity_main.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
3.MainActivity
public class MainActivity extends AppCompatActivity { private MyReceiverD receiverD; private MyReceiverD_zdy receiverDZdy; private Button bnt; private EditText etxt; private TextView txt; public static final String MY_ACTION = "com.example.my"; public static final String BROADCAST_COnTENT= "cs"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //设置应用主页面的标题 setTitle(getPackageName()); //新建广播接收器 receiverDZdy = new MyReceiverD_zdy(txt); //注册广播接收器 //为广播添加Action IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("android.intent.action,PACKAGE_REMOVED"); //自定义 intentFilter.addAction(MY_ACTION); //注册广播接收器 registerReceiver(receiverDZdy,intentFilter); bnt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //新建广播 自定义 Intent intent = new Intent(MY_ACTION); //携带数据 intent.putExtra(BROADCAST_CONTENT,etxt.getText().toString()); //发送广播 sendBroadcast(intent); } }); } protected void onDestroy() { super.onDestroy(); //取消注册关闭接收器 if (receiverDZdy != null){ unregisterReceiver(receiverDZdy); } } private void initView() { //初始化 etxt = (EditText) findViewById(R.id.etxt); txt =(TextView) findViewById(R.id.txt); bnt =(Button) findViewById(R.id.bnt); } }
样式
当然也可以实现不同app接受发送的广播内容
换app名字,当前app发送的广播新的app也可以接收到
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。