您好,登录后才能下订单哦!
这篇文章给大家介绍 BroadcastReceiver怎么在Android中使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
主要代码
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里设置权限
<receiver android:name=".MyReceiver"> <!--接受广播类型--> <intent-filter> <!--开机广播--> <action android:name="android.intent.action.BOOT_COMPLETED"/> <!--电量低广播--> <action android:name="android.intent.action.BATTERY_LOW"/> <!--应用卸载--> <action android:name="android.intent.action.PACKAGE_REMOVED"/> <!--应用安装--> <action android:name="android.intent.action.PACKAGE_INSTALL"/> <!--数据类型--> <data android:scheme="package"/> </intent-filter> </receiver>
动态的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里设置权限
<!--动态注册--> <receiver android:name=".MyReceiverD"> //因为是动态设置就不需要在里面设置别的了 </receiver>
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
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:padding="16dp" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请输入发送内容:"/> <EditText android:id="@+id/etxt" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="16dp" /> <Button android:id="@+id/bnt" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_gravity="center_horizontal" android:text="发送广播"/> <TextView android:id="@+id/txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="收到的内容:"/> </LinearLayout>
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);
    }
}关于 BroadcastReceiver怎么在Android中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。