zoukankan      html  css  js  c++  java
  • 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>
    物随心转,境由心造,一切烦恼皆由心生。
  • 相关阅读:
    Python推导式(Comprehension)
    mysql中文乱码
    入门学习hibernate
    什么是ORM?
    Java网站中的权限管理
    Java的8中基本数据类型
    Python获取文件夹大小
    Python技巧
    Python中取整的方法floor,ceil,round
    Python线程join和setDaemon
  • 原文地址:https://www.cnblogs.com/woodrow2015/p/4531756.html
Copyright © 2011-2022 走看看