015_01自定义广播

  

  Broadcast的类型有两种:普通广播和有序广播。

  Normal broadcasts(普通广播):Normal broadcasts是完全异步的可以同一时间被所有的接收者接收到。消息的传递效率比较高。但缺点是接收者不能讲接收的消息的处理信息传递给下一个接收者也不能停止消息的传播。

   Ordered broadcasts(有序广播):Ordered broadcasts的接收者按照一定的优先级进行消息的接收。如:A,B,C的优先级依次降低,那么消息先传递给A,在传递给B,最后传递给C。优先级别声明在中,取值为[-1000,1000]数值越大优先级别越高。优先级也可通过filter.setPriority(10)方式设置。 另外Ordered broadcasts的接收者可以通过abortBroadcast()的方式取消广播的传播,也可以通过setResultData和setResultExtras方法将处理的结果存入到Broadcast中,传递给下一个接收者。然后,下一个接收者通过getResultData()和getResultExtras(true)接收高优先级的接收者存入的数据。

 1 package com.example.activity;
 2
 3 import com.example.broadcast.MyBroadcastReceiver;
 4 import com.example.broadcast.WeatherBureau;
 5 import com.example.day_15broadcastreceiverdemo.R;
 6 import android.app.Activity;
 7 import android.content.Intent;
 8 import android.content.IntentFilter;
 9 import android.os.Bundle;
10 import android.view.View;
11
12 public class MyActivity extends Activity{
13     MyBroadcastReceiver receiver = null;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.main_activity);
18     }
19
20     //动态注册接收短信广播
21     public void register(View v){
22         /*    <receiver android:name="com.example.smsforward.MySmsReceiver">
23         <intent-filter android:priority="1000">
24             <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
25         </intent-filter>
26         </receiver>*/
27
28         //动态注册后程序运行才可以监听广播;当应用退出,将无法继续监听广播。
29         receiver = new MyBroadcastReceiver();
30         IntentFilter  intentFilter = new IntentFilter();
31         intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
32         intentFilter.setPriority(500);
33         registerReceiver(receiver, intentFilter);
34     }
35
36     //动态注册自定义广播
37     public void register2(View v){
38         WeatherBureau receiver2 = new WeatherBureau();
39         IntentFilter  intentFilter = new IntentFilter();
40         intentFilter.addAction("com.woodrow.weatherforcast");
41         registerReceiver(receiver2, intentFilter);
42     }
43
44     //发送自定义无序广播
45     public void sendunorder(View v){
46          //发送无序广播
47         Intent intent = new Intent();
48         intent.setAction("com.woodrow.weatherforcast");
49         sendBroadcast(intent);
50     }
51
52     //发送自定义有序广播
53     public void sendorder(View v){
54         //发送有序广播
55         Intent intent = new Intent();
56         intent.setAction("com.woodrow.weatherforcast");
57         WeatherBureau wb = new WeatherBureau();
58         sendOrderedBroadcast(intent, null, wb, null, 100, "暴雨,风力十二级", null);
59     }
60
61     @Override
62     protected void onDestroy() {
63         // TODO Auto-generated method stub
64         super.onDestroy();
65         if(receiver != null){
66             unregisterReceiver(receiver);
67         }
68     }
69 }
 1 package com.example.broadcast;
 2
 3 import android.content.BroadcastReceiver;
 4 import android.content.Context;
 5 import android.content.Intent;
 6
 7 public class WeatherBureau extends BroadcastReceiver {
 8     @Override
 9     public void onReceive(Context arg0, Intent arg1) {
10         int code = getResultCode();
11         String data =getResultData();
12         String action = arg1.getAction();
13         if ("com.woodrow.weatherforcast".equals(action)) {
14              System.out.println("WeatherBureau.onReceive() " + code +":"+data);
15              setResultData("今晚有雷雨大风!");
16         }
17     }
18 }
 1 package com.example.broadcast;
 2
 3 import android.content.BroadcastReceiver;
 4 import android.content.Context;
 5 import android.content.Intent;
 6
 7 public class Level1Revceiver extends BroadcastReceiver {
 8
 9     @Override
10     public void onReceive(Context arg0, Intent arg1) {
11         // TODO Auto-generated method stub
12          int code = getResultCode();
13          String data =getResultData();
14          System.out.println("Level1Revceiver.onReceive() 市政府收到通知" + code +":"+data );
15          setResultData("今晚有雷雨大风,各部门注意!");
16
17     }
18
19 }
 1 package com.example.broadcast;
 2
 3 import android.content.BroadcastReceiver;
 4 import android.content.Context;
 5 import android.content.Intent;
 6
 7 public class Level2Revceiver extends BroadcastReceiver {
 8
 9     @Override
10     public void onReceive(Context arg0, Intent arg1) {
11         // TODO Auto-generated method stub
12
13          int code = getResultCode();
14          String data =getResultData();
15          System.out.println("Level2Revceiver.onReceive() 区政府收到通知" + code +":"+data );
16          setResultData("今晚有雷雨大风,请安全出行!");
17     }
18
19 }
 1 package com.example.broadcast;
 2
 3 import android.content.BroadcastReceiver;
 4 import android.content.Context;
 5 import android.content.Intent;
 6
 7 public class Level3Revceiver extends BroadcastReceiver {
 8
 9     @Override
10     public void onReceive(Context arg0, Intent arg1) {
11         // TODO Auto-generated method stub
12
13          int code = getResultCode();
14            String data =getResultData();
15            System.out.println("Level3Revceiver.onReceive() xx大学收到通知" + code +":"+data );
16            setResultData("今晚有雷雨大风,请不要外出");
17     }
18
19 }
 1 package com.example.broadcast;
 2
 3 import android.content.BroadcastReceiver;
 4 import android.content.Context;
 5 import android.content.Intent;
 6
 7 public class Level4Revceiver extends BroadcastReceiver {
 8
 9     @Override
10     public void onReceive(Context arg0, Intent arg1) {
11            int code = getResultCode();
12              String data =getResultData();
13            System.out.println("Level4Revceiver.onReceive() 学生收到通知" + code +":"+data );
14            abortBroadcast();
15     }
16 }
 1 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 2     package="com.example.day_15broadcastreceiverdemo"
 3     android:versionCode="1"
 4     android:versionName="1.0" >
 5
 6     <uses-sdk
 7         android:minSdkVersion="8"
 8         android:targetSdkVersion="9" />
 9     <uses-permission android:name="android.permission.RECEIVE_SMS"/>
10
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity  android:name="com.example.activity.MyActivity">
17             <intent-filter >
18                 <action android:name="android.intent.action.MAIN"/>
19                 <category android:name="android.intent.category.LAUNCHER"/>
20             </intent-filter>
21         </activity>
22         <receiver android:name="com.example.broadcast.MyBroadcastReceiver"></receiver>
23
24         <receiver android:name="com.example.broadcast.WeatherBureau">
25              <intent-filter android:priority="1000">
26                 <action android:name="com.woodrow.weatherforcast"/>
27              </intent-filter>
28         </receiver>
29         <receiver android:name="com.example.broadcast.Level1Revceiver">
30              <intent-filter android:priority="500">
31                  <action android:name="com.woodrow.weatherforcast"/>
32              </intent-filter>
33         </receiver>
34         <receiver android:name="com.example.broadcast.Level2Revceiver">
35              <intent-filter android:priority="50">
36                  <action android:name="com.woodrow.weatherforcast"/>
37              </intent-filter>
38         </receiver>
39         <receiver android:name="com.example.broadcast.Level3Revceiver">
40              <intent-filter android:priority="-500">
41                  <action android:name="com.woodrow.weatherforcast"/>
42              </intent-filter>
43         </receiver>
44         <receiver android:name="com.example.broadcast.Level4Revceiver">
45              <intent-filter android:priority="-1000">
46                  <action android:name="com.woodrow.weatherforcast"/>
47              </intent-filter>
48         </receiver>
49
50     </application>
51
52 </manifest>
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6
 7
 8     <EditText
 9            android:id="@+id/et_name"
10            android:layout_width="fill_parent"
11            android:layout_height="wrap_content"
12         />
13
14         <Button
15            android:id="@+id/bt_save"
16            android:layout_width="fill_parent"
17            android:layout_height="wrap_content"
18            android:text="动态注册接收短信广播"
19            android:onClick="register"
20         />
21
22
23                     <Button
24             android:layout_width="fill_parent"
25            android:layout_height="wrap_content"
26            android:text="动态注册自定义广播"
27            android:onClick="register2"
28         />
29             <Button
30             android:layout_width="fill_parent"
31            android:layout_height="wrap_content"
32            android:text="发送自定义无序广播"
33            android:onClick="sendunorder"
34         />
35            <Button
36             android:layout_width="fill_parent"
37            android:layout_height="wrap_content"
38            android:text="发送自定义有序广播"
39            android:onClick="sendorder"
40         />
41 </LinearLayout>
时间: 2024-08-10 15:08:53

015_01自定义广播的相关文章

Android 自定义广播发送和接收

android系统会发送许多系统级别的广播,比如屏幕关闭,电池电量低等广播.同样应用可以发起自定义“由开发者定义的”广播.广播是从一个应用内部向另一个应用发送消息的途径之一. BroadcastReceiver是一个可以监听和响应广播的组件.本文中,我们将会演示如何发送自定义广播以及如何通过编程和使用Manifest文件定义一个BroadcastReceiver来监听这一广播.我们最后只要调用sendBroadcast就可以发送广播信息了. 1,编写MyReceiver,MyReceiver代码

自定义广播

自定义广播 自定义广播就是我们自己来写广播发送者,也自己来写广播接收者. 效果图: 分析: 1.自己写好广播发送者 设置广播的id,广播接收者监听的时候需要监听这个 intent.setAction("com.fry.receiver"); 这是给广播接收者带数据,没这个也行 intent.putExtra("key", "这是广播发送者给接收者带的数据"); 发送广播 sendBroadcast(intent); 给广播设置权限,设置了这个权限

MIUI7,Android版本5.0.2,一个程序发送自定义广播,另一个程序没有接收到

对照<第一行代码——Android>进行学习,第五章中说到广播包的相关知识,前面获取广播等程序例程都可以跑的通,但是在5.3.2节中,程序A发送自定义广播,并接收自定义广播,同时程序B也接收该自定义广播.实际编写代码测试程序A发送之后只有程序A收到了改自定义广播,程序B并没有接收到,我认为是我工程配置的问题,因此下载了书本中的例程直接跑,现象任然是这样,程序A发送广播之后只有程序A可以收到,程序B没有收到. 不知道是什么原因,测试的手机是小米2s,系统MIUI7,Android版本5.0.2.

自定义广播发送、接收

//发送自定义广播 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 发送广播事件 (消息) * @param view */ public void click(V

【android基础篇】自定义广播和电话监听

I,自定义广播 前面所说的都是接收短信,外拨电话等都是系统所有的广播,而其实我们可以自己自定义一个广播,并且写一个广播接收者来玩玩. 1) 在按钮的点击方法中,发送自定义的广播: 1 public void click(View view){ 2 /** 3 * 发送自定义的广播 4 */ 5 Intent intent=new Intent(); 6 //设置意图的动作,要和自定义的频道要一致 7 intent.setAction("www.wangchengxiang.com");

自定义广播和广播优先级

1.发送广播代码 1 package com.example.alame; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.view.View; 7 8 public class MainActivity extends Activity { 9 10 @Override 11 protected void onCreate

自定义广播Broadcast

/*发送一个自定义广播 * 指定广播目标Action * 可通过Intent携带消息 * 发送广播消息 */ private void sendMyBroadcast(){ Intent intent = new Intent("MyReceiver_Action"); intent.putExtra("msg", "发送自定义的广播"); sendBroadcast(intent); } //自己写的一个广播类 public class Bro

android#boardcast#发送自定义广播

广播主要分为两种类型,标准广播和有序广播,通过实践的方式来看下这两种广播具体的区别. 一.发送标准广播 在发送广播之前,我们还是需要先定义一个广播接收器来准备接收此广播才行,不然发出去也是白发.因此新建一个MyBroadcastReceiver继承自BroadcastReceiver,代码如下所示: public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Contex

Android(java)学习笔记178:自定义广播

广播使用:               电台:对外发送信号.---------电台发送广播(可以自定义)               收音机:接收电台的信号.-----广播接收者 这里,我们就说明自定义电台,搭建属于自己的电台广播: 1.首先我们编写自己的电台android 程序,搭建电台,如下: (1)首先是布局文件,activity_main.xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res