BroadCast用法以及notification用法

时间:2023-01-30 21:28:40

---恢复内容开始---

今天学习了broadcast,并且完成了一个简单的demo。

broadcast类似于向所有的安卓应用发出一个命令,但不是所有的应用都可以相应这个命令,只有注册了广播的应用才可以,还需要定义一个broadcastreceiver,在onreceive里执行应用在接受广播之后要执行的方法。

具体步骤如下:

  • 1:自定义一个BroadcastReceiver,在onReceive()方法中完成广播要处理的事务
  • 2: (1)动态注册广播:(程序启动时才可以接受)

         mBroadcastReciever = new MyBroadcastReciever();

        IntentFilter intentFilter = new IntentFilter();

        intentFilter.addAction("com.example.vita.broadcastnotification");//动态添加action,注册

        registerReceiver(mBroadcastReciever,intentFilter);

 

   (记得在onDestroy里要取消广播哦)

  unregisterReceiver(mBroadcastReciever);

 

    (2)静态注册广播:(程序还没启动都可以接受,3.1系统有变化,sendintent前要设置一个flag)

    

    onrecieve方法里面:

    if (ACTION_BOOT.equals(intent.getAction())){

          Toast.makeText(context, "开机完毕~", Toast.LENGTH_LONG).show();}

 

    在AndroidManifest.xml中对该BroadcastReceiver进行注册,添加开机广播的intent-filter!

    对了,别忘了加上android.permission.RECEIVE_BOOT_COMPLETED的权限哦!

 

    receiver android:name=".BootCompleteReceiver">

        <intent-filter>

            <action android:name = "android.intent.cation.BOOT_COMPLETED">

        </intent-filter>

    </receiver>

 

    <!-- 权限 -->

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

  • 3:定义BroadcastReceiver类(继承于BroadcastReceiver类),重写onReceive方法:(不要在广播里添加过多逻辑或者进行任何耗时操作,因为在广播中是不允许开辟线程的, 当onReceiver( )方法运行较长时间(超过10秒)还没有结束的话,那么程序会报错(ANR), 广播更多的时候扮演的是一个打开其他组件的角色,比如启动Service,Notification提示, Activity等!)

  

 public void onReceive(Context context, Intent intent) {
Log.d(TAG,
"intent "+intent);
sendNotification(context);

}


// @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void sendNotification(Context context){
notifyMgr
= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
builder1
= new NotificationCompat.Builder(context);

builder1.setSmallIcon(R.mipmap.ic_launcher);
builder1.setContentTitle(
"提示");
builder1.setContentText(
"enjoy summer days");
builder1.setSubText(
"notification");
builder1.setTicker(
"this is a notification");
builder1.setAutoCancel(
false);

Intent mIntent
= new Intent(context,MainActivity.class);
Log.d(TAG,
"contexd: "+context+";Otheractivity.class:"+OtherActivity.class);
PendingIntent pi
= PendingIntent.getActivity(context,0, mIntent,0);
builder1.setContentIntent(pi);
Notification notify1
=builder1.build();

notifyMgr.notify(
1,notify1);
}
  • 4.:发送broadcast:新建一个项目,此项目通过Intent里的action寻找可以接受广播的应用。
     button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    sendBroadcast(
    new Intent("com.example.vita.broadcastnotification"));
    }
    });

     

broadcast用法介绍完毕;

上面介绍的是全局广播,也就是本app发送的广播,整个系统app都可以收到,非常不安全。所以谷歌还给我们准备了本地广播。

本地广播只能在本App中传播,并且广播接收器也只能接收来自本APP发出的广播,所以在该情况下可防止其他应用获取广播中的敏感数据从而确保数据安全。(本地广播无法通过静态注册方式来接受,相比起系统全局广播更加高效)

使用流程:

1.获取localBroadcastManager

localBroadcastManager = LocalBroadcastManager.getInstance(this);

2.获取localReceiver

localReceiver = new MyBcReceiver();

3.动态注册

4.localBroadcastManager注册广播接收者

localBroadcastManager.registerReceiver(localReceiver, intentFilter);

5.在destroy中,注销广播注册者。(registerReceiver生命周期与ui线程一样长,所以activity ondestroy将其注销掉,回收内存)

 localBroadcastManager.unregisterReceiver(localReceiver);

demo里涉及的notification用法如下:

#######notification使用流程#########

  • Step 1. 获得NotificationManager对象: NotificationManager mNManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  • Step 2. 创建一个通知栏的Builder构造类: NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);(为了兼容低版本安卓系统,用NotificationCompat而不是Notification)
  •  Step 3.Builder进行相关的设置,比如标题,内容,图标,动作等!
  • Step 4.调用Builder的build()方法为notification赋值
  • Step 5.调用NotificationManager的notify()方法发送通知!
  • PS:另外我们还可以调用NotificationManager的cancel()方法取消通知
 private void  sendNotification(Context context){
notifyMgr
= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
builder1
= new NotificationCompat.Builder(context);

builder1.setSmallIcon(R.mipmap.ic_launcher);
builder1.setContentTitle(
"提示");
builder1.setContentText(
"enjoy summer days");
builder1.setSubText(
"notification");
builder1.setTicker(
"this is a notification");
builder1.setAutoCancel(
false);

Intent mIntent
= new Intent(context,MainActivity.class);
Log.d(TAG,
"contexd: "+context+";Otheractivity.class:"+OtherActivity.class);
PendingIntent pi
= PendingIntent.getActivity(context,0, mIntent,0);
builder1.setContentIntent(pi);
Notification notify1
=builder1.build();

notifyMgr.notify(
1,notify1);
}

 

---恢复内容结束---