android广播

android广播:

普通广播:

1.在AndroidManifest.xml中配置广播接收器:

       <receiver android:name="com.example.toast.MyBroadReceiver" >
            <intent-filter>
                <action android:name="MyBroad" />
            </intent-filter>
        </receiver>

2.需要继承一个BroadcastReceiver对象

3.将消息通过intent方法传递出去

4.最后需要调用context的sendBroadcast(intent)方法

有序广播:与常规广播一样,但是有序广播拥有优先级,需要多个接收器,而传递的消息可以在任何一处终止,也可以在任何一处添加消息,但是传播的消息需要以包()的形式传递,

比如:

        Bundle bundle=new Bundle();
        bundle.putString("mes2", "01已收到");
        setResultExtras(bundle);

而接收器需要得到包中的消息需要用Bundle bundle = getResultExtras(true);接收。

系统广播:

通知(Notification):

1.主要涉及的3大类:

Notification.builder用于动态的Notification的属性set来设置

NotificationManager主要是通知的显示和取消显示

Notification设置Notification的相关属性

2.发送Notification需要

1).调用getSystemService(NOTIFICATION_SERVICE)方法获取系统的NotificationManager服务

2).创建Notification对象

3).设置Notification属性

4).通过NotificationManager发送Notification

5).发送notify  取消cancel


猜你喜欢

转载自blog.csdn.net/qq_41851370/article/details/79584253