广播接收器 broadcast sendOrderedBroadcast android

摘自:http://blog.csdn.net/woaieillen/article/details/7373090

广播接收器:

 

广播接收者(BroadcastReceiver)用于监听系统事件或应用程序事件,通过调用Context.sendBroadcast()、Context.sendOrderedBroadcast()可以向系统发送广播意图,通过广播一个意图(Intent)可以被多个广播接收者所接收,从而可以在不用修改原始的应用程序的情况下,让你对事件作出反应。

       其中Context.sendBroad()主要是用来广播无序事件(也被称为有序广播 Normal broadcast),即所有的接收者在理论上是同时接收到事件,同时执行的,对消息传递的效率而言这是比较好的做法。而Context.sendOrderedBroadcast()方法用来向系统广播有序事件(Ordered broadcast),接收者按照在Manifest.xml文件中设置的接收顺序依次接收Intent,顺序执行的,接收的优先级可以在系统配置文件中设置(声明在intent-filter元素的android:priority属性中,数值越大优先级别越高,其取值范围为-1000到1000。当然也可以在调用IntentFilter对象的setPriority()方法进行设置)。对于有序广播而言,前面的接收者可以对接收到得广播意图(Intent)进行处理,并将处理结果放置到广播意图中,然后传递给下一个接收者,当然前面的接收者有权终止广播的进一步传播。如果广播被前面的接收者终止后,后面的接收器就再也无法接收到广播了。

 

广播接收器(Broadcaset)运行的线程:

       无论对于有序广播还是无序广播,广播接收器默认都是运行在主线程中的(main线程,即UI线程)。可以通过在程序中使用registerReceiver(receiver, filter, broadcastPermission, scheduler)方法中的最后一个参数指定要运行的广播接收器的线程。也可以在Manifest.xml文件中设置(Intent-filter标签中设置android:process)。

 

无序广播(Normal Broadcast)

       基本步骤:写一个类继承BroadcastReceiver,并重写onReceive方法,而后在AndroidManifest.xml文中中进行配置,或者直接在代码中注册。

      

下面是一个广播接收器的Demo(用于发送和接收短信):

      

  1. import android.content.BroadcastReceiver;  
  2. import android.content.Context;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.telephony.SmsMessage;  
  6. import android.util.Log;  
  7.    
  8. public class ReceivingSMSReceiver extends BroadcastReceiver {  
  9.     private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";  
  10.     private static final String TAG = "ReceivingSMSReceiver";  
  11.    
  12.     @Override  
  13.     public void onReceive(Context context, Intent intent) {  
  14.        if (intent.getAction().equals(SMS_RECEIVED)) {  
  15.            Bundle bundle = intent.getExtras();  
  16.            if (bundle != null) {  
  17.               Object[] pdus = (Object[]) bundle.get("pdus");  
  18.               SmsMessage[] messages = new SmsMessage[pdus.length];  
  19.               for (int i = 0; i < pdus.length; i++)  
  20.                   messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);  
  21.               for (SmsMessage message : messages) {  
  22.                   String msg = message.getMessageBody();  
  23.                   Log.i(TAG, msg);  
  24.                   String to = message.getOriginatingAddress();  
  25.                   Log.i(TAG, to);  
  26.               }  
  27.            }  
  28.        }  
  29.     }  
  30. }  
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
 
public class ReceivingSMSReceiver extends BroadcastReceiver {
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    private static final String TAG = "ReceivingSMSReceiver";
 
    @Override
    public void onReceive(Context context, Intent intent) {
       if (intent.getAction().equals(SMS_RECEIVED)) {
           Bundle bundle = intent.getExtras();
           if (bundle != null) {
              Object[] pdus = (Object[]) bundle.get("pdus");
              SmsMessage[] messages = new SmsMessage[pdus.length];
              for (int i = 0; i < pdus.length; i++)
                  messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
              for (SmsMessage message : messages) {
                  String msg = message.getMessageBody();
                  Log.i(TAG, msg);
                  String to = message.getOriginatingAddress();
                  Log.i(TAG, to);
              }
           }
       }
    }
}

 

在AndroidManifest.xml文件中的<application>节点里对接收到短信的广播Intent进行注册:

<receiver android:name=". ReceivingSMSReceiver">

<intent-filter>

<action android:name="android.provider.Telephony.SMS_RECEIVED"/>

</intent-filter>

</receiver>

在AndroidManifest.xml文件中添加以下权限:

<uses-permission android:name="android.permission.RECEIVE_SMS"/><!-- 接收短信权限 -->

 

该广播接收器将首先得到本机收到的短信,可以对短信内容进行过滤。

 

在模拟器中运行该工程。

 

建立一个新的Android工程,新建一个Activity用来发送短信:

  1. import android.app.Activity;  
  2. import android.app.PendingIntent;  
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.content.IntentFilter;  
  7. import android.os.Bundle;  
  8. import android.telephony.SmsManager;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12. import android.widget.Toast;  
  13.    
  14. public class SMSSender extends Activity {  
  15.     private static final String TAG = "SMSSender";  
  16.     Button send = null;  
  17.     EditText address = null;  
  18.     EditText content = null;  
  19.     private mServiceReceiver mReceiver01, mReceiver02;  
  20.     private static String SEND_ACTIOIN = "SMS_SEND_ACTION";  
  21.     private static String DELIVERED_ACTION = "SMS_DELIVERED_ACTION";  
  22.    
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.        super.onCreate(savedInstanceState);  
  26.        setContentView(R.layout.main);  
  27.        address = (EditText) findViewById(R.id.address);  
  28.        content = (EditText) findViewById(R.id.content);  
  29.        send = (Button) findViewById(R.id.send);  
  30.        send.setOnClickListener(new View.OnClickListener() {  
  31.    
  32.            @Override  
  33.            public void onClick(View v) {  
  34.               // TODO Auto-generated method stub   
  35.               String mAddress = address.getText().toString().trim();  
  36.               String mContent = content.getText().toString().trim();  
  37.               if ("".equals(mAddress) || "".equals(mContent)) {  
  38.                   Toast.makeText(SMSSender.this"发送地址为空或内容为空!",  
  39.                          Toast.LENGTH_LONG).show();  
  40.                   return;  
  41.               }  
  42.               SmsManager smsManager = SmsManager.getDefault();  
  43.               try {  
  44.                   Intent send_Intent = new Intent(SEND_ACTIOIN);  
  45.                   Intent deliver_Intent = new Intent(DELIVERED_ACTION);  
  46.                   PendingIntent mSend = PendingIntent.getBroadcast(  
  47.                          getApplicationContext(), 0, send_Intent, 0);  
  48.                   PendingIntent mDeliver = PendingIntent.getBroadcast(  
  49.                          getApplicationContext(), 0, deliver_Intent, 0);  
  50.                   smsManager.sendTextMessage(mAddress, null, mContent, mSend,  
  51.                          mDeliver);  
  52.               } catch (Exception e) {  
  53.                   e.printStackTrace();  
  54.               }  
  55.            }  
  56.        });  
  57.     }  
  58.    
  59.     @Override  
  60.     protected void onResume() {  
  61.        // TODO Auto-generated method stub   
  62.        IntentFilter mFilter01;  
  63.        mFilter01 = new IntentFilter(SEND_ACTIOIN);  
  64.        mReceiver01 = new mServiceReceiver();  
  65.        registerReceiver(mReceiver01, mFilter01);  
  66.        mFilter01 = new IntentFilter(DELIVERED_ACTION);  
  67.        mReceiver02 = new mServiceReceiver();  
  68.        registerReceiver(mReceiver02, mFilter01);  
  69.        super.onResume();  
  70.     }  
  71.    
  72.     @Override  
  73.     protected void onPause() {  
  74.        // TODO Auto-generated method stub   
  75.        unregisterReceiver(mReceiver01);  
  76.        unregisterReceiver(mReceiver02);  
  77.        super.onPause();  
  78.     }  
  79.    
  80.     public class mServiceReceiver extends BroadcastReceiver {  
  81.        @Override  
  82.        public void onReceive(Context context, Intent intent) {  
  83.            // TODO Auto-generated method stub   
  84.            if (intent.getAction().equals(SEND_ACTIOIN)) {  
  85.               try {  
  86.                   switch (getResultCode()) {  
  87.                   case Activity.RESULT_OK:  
  88.                      break;  
  89.                   case SmsManager.RESULT_ERROR_GENERIC_FAILURE:  
  90.                      break;  
  91.                   case SmsManager.RESULT_ERROR_RADIO_OFF:  
  92.                      break;  
  93.                   case SmsManager.RESULT_ERROR_NULL_PDU:  
  94.                      break;  
  95.                   }  
  96.               } catch (Exception e) {  
  97.                   e.getStackTrace();  
  98.               }  
  99.            } else if (intent.getAction().equals(DELIVERED_ACTION)) {  
  100.               try {  
  101.                   switch (getResultCode()) {  
  102.                   case Activity.RESULT_OK:  
  103.                      break;  
  104.                   case SmsManager.RESULT_ERROR_GENERIC_FAILURE:  
  105.                      break;  
  106.                   case SmsManager.RESULT_ERROR_RADIO_OFF:  
  107.                      break;  
  108.                   case SmsManager.RESULT_ERROR_NULL_PDU:  
  109.                      break;  
  110.                   }  
  111.               } catch (Exception e) {  
  112.                   e.getStackTrace();  
  113.               }  
  114.            }  
  115.        }  
  116.     }  
  117. }  
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class SMSSender extends Activity {
    private static final String TAG = "SMSSender";
    Button send = null;
    EditText address = null;
    EditText content = null;
    private mServiceReceiver mReceiver01, mReceiver02;
    private static String SEND_ACTIOIN = "SMS_SEND_ACTION";
    private static String DELIVERED_ACTION = "SMS_DELIVERED_ACTION";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       address = (EditText) findViewById(R.id.address);
       content = (EditText) findViewById(R.id.content);
       send = (Button) findViewById(R.id.send);
       send.setOnClickListener(new View.OnClickListener() {
 
           @Override
           public void onClick(View v) {
              // TODO Auto-generated method stub
              String mAddress = address.getText().toString().trim();
              String mContent = content.getText().toString().trim();
              if ("".equals(mAddress) || "".equals(mContent)) {
                  Toast.makeText(SMSSender.this, "发送地址为空或内容为空!",
                         Toast.LENGTH_LONG).show();
                  return;
              }
              SmsManager smsManager = SmsManager.getDefault();
              try {
                  Intent send_Intent = new Intent(SEND_ACTIOIN);
                  Intent deliver_Intent = new Intent(DELIVERED_ACTION);
                  PendingIntent mSend = PendingIntent.getBroadcast(
                         getApplicationContext(), 0, send_Intent, 0);
                  PendingIntent mDeliver = PendingIntent.getBroadcast(
                         getApplicationContext(), 0, deliver_Intent, 0);
                  smsManager.sendTextMessage(mAddress, null, mContent, mSend,
                         mDeliver);
              } catch (Exception e) {
                  e.printStackTrace();
              }
           }
       });
    }
 
    @Override
    protected void onResume() {
       // TODO Auto-generated method stub
       IntentFilter mFilter01;
       mFilter01 = new IntentFilter(SEND_ACTIOIN);
       mReceiver01 = new mServiceReceiver();
       registerReceiver(mReceiver01, mFilter01);
       mFilter01 = new IntentFilter(DELIVERED_ACTION);
       mReceiver02 = new mServiceReceiver();
       registerReceiver(mReceiver02, mFilter01);
       super.onResume();
    }
 
    @Override
    protected void onPause() {
       // TODO Auto-generated method stub
       unregisterReceiver(mReceiver01);
       unregisterReceiver(mReceiver02);
       super.onPause();
    }
 
    public class mServiceReceiver extends BroadcastReceiver {
       @Override
       public void onReceive(Context context, Intent intent) {
           // TODO Auto-generated method stub
           if (intent.getAction().equals(SEND_ACTIOIN)) {
              try {
                  switch (getResultCode()) {
                  case Activity.RESULT_OK:
                     break;
                  case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                     break;
                  case SmsManager.RESULT_ERROR_RADIO_OFF:
                     break;
                  case SmsManager.RESULT_ERROR_NULL_PDU:
                     break;
                  }
              } catch (Exception e) {
                  e.getStackTrace();
              }
           } else if (intent.getAction().equals(DELIVERED_ACTION)) {
              try {
                  switch (getResultCode()) {
                  case Activity.RESULT_OK:
                     break;
                  case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                     break;
                  case SmsManager.RESULT_ERROR_RADIO_OFF:
                     break;
                  case SmsManager.RESULT_ERROR_NULL_PDU:
                     break;
                  }
              } catch (Exception e) {
                  e.getStackTrace();
              }
           }
       }
    }
}


界面布局:

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <TextView android:layout_width="fill_parent"  
  6.        android:layout_height="wrap_content" android:text="发送短信" />  
  7.     <TextView android:text="收信人地址:" android:id="@+id/textView1"  
  8.        android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>  
  9.     <EditText android:text="" android:layout_width="match_parent"  
  10.        android:id="@+id/address" android:layout_height="wrap_content"></EditText>  
  11.     <TextView android:text="短信内容:" android:id="@+id/textView2"  
  12.        android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>  
  13.     <EditText android:text="" android:layout_width="match_parent"  
  14.        android:id="@+id/content" android:layout_height="wrap_content"></EditText>  
  15.     <Button android:text="发送" android:id="@+id/send"  
  16.        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>  
  17. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
       android:layout_height="wrap_content" android:text="发送短信" />
    <TextView android:text="收信人地址:" android:id="@+id/textView1"
       android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <EditText android:text="" android:layout_width="match_parent"
       android:id="@+id/address" android:layout_height="wrap_content"></EditText>
    <TextView android:text="短信内容:" android:id="@+id/textView2"
       android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <EditText android:text="" android:layout_width="match_parent"
       android:id="@+id/content" android:layout_height="wrap_content"></EditText>
    <Button android:text="发送" android:id="@+id/send"
       android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>


AndroidManifest.xml文件中增加对该Activity的配置:

 

 

  1. <activity android:name=".SMSSender" android:label="@string/app_name">  
  2.            <intent-filter>  
  3.               <action android:name="android.intent.action.MAIN" />  
  4.               <category android:name="android.intent.category.LAUNCHER" />  
  5.            </intent-filter>  
  6.        </activity>  
<activity android:name=".SMSSender" android:label="@string/app_name">
           <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>

 

 

在AndroidManifest.xml文件中添加以下权限:

<uses-permission android:name="android.permission.SEND_SMS"/><!—发送短信权限 -->

 

在另一个模拟器中运行该工程。

 

由于本文的重点内容并不是发送和接收短信,所以,对短信发送和接收的内容并没有详细解释。如果对短信收发内容不熟悉的朋友,可以查阅相关文档。

 

 

有序广播:

 

接收器1:

  1. import android.content.BroadcastReceiver;  
  2. import android.content.Context;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5.    
  6. public class BroadcastTest2 extends BroadcastReceiver {  
  7.    
  8.     @Override  
  9.     public void onReceive(Context arg0, Intent arg1) {  
  10.        System.out.println("运行线程:    " + Thread.currentThread().getId() + "   "  
  11.               + Thread.currentThread().getName());  
  12.        System.out.println("广播意图的动作:  " + arg1.getAction());  
  13.        Bundle bundle = new Bundle();  
  14.        bundle.putString("test""zhongnan");  
  15.        setResultExtras(bundle);  
  16.     }  
  17. }  
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
 
public class BroadcastTest2 extends BroadcastReceiver {
 
    @Override
    public void onReceive(Context arg0, Intent arg1) {
       System.out.println("运行线程:    " + Thread.currentThread().getId() + "   "
              + Thread.currentThread().getName());
       System.out.println("广播意图的动作:  " + arg1.getAction());
       Bundle bundle = new Bundle();
       bundle.putString("test", "zhongnan");
       setResultExtras(bundle);
    }
}

接收器2

 

  1. import android.content.BroadcastReceiver;  
  2. import android.content.Context;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5.    
  6. public class BroadcastTest1 extends BroadcastReceiver {  
  7.    
  8.     @Override  
  9.     public void onReceive(Context arg0, Intent arg1) {  
  10.        System.out.println("运行线程:    " + Thread.currentThread().getId() + "   "  
  11.               + Thread.currentThread().getName());  
  12.        System.out.println("广播意图的动作:  " + arg1.getAction());  
  13.        Bundle bundle = getResultExtras(false);  
  14.         
  15.        if (bundle == null) {  
  16.            System.out.println("没有得到上次传递的数据");  
  17.        } else {  
  18.            System.out.println("测试:  " + bundle.getString("test"));  
  19.        }  
  20.     }  
  21. }  
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
 
public class BroadcastTest1 extends BroadcastReceiver {
 
    @Override
    public void onReceive(Context arg0, Intent arg1) {
       System.out.println("运行线程:    " + Thread.currentThread().getId() + "   "
              + Thread.currentThread().getName());
       System.out.println("广播意图的动作:  " + arg1.getAction());
       Bundle bundle = getResultExtras(false);
      
       if (bundle == null) {
           System.out.println("没有得到上次传递的数据");
       } else {
           System.out.println("测试:  " + bundle.getString("test"));
       }
    }
}

 

 

Activity,用来发送广播:

 

  1. import android.app.Activity;  
  2. import android.content.Intent;  
  3. import android.os.Bundle;  
  4.    
  5. public class MainActivity extends Activity {  
  6.     /** Called when the activity is first created. */  
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.        super.onCreate(savedInstanceState);  
  10.        setContentView(R.layout.main);  
  11.        this.sendOrderedBroadcast(new Intent(  
  12.               "android.provier.zhongnan.broadcast"), null);  
  13.     }  
  14. }  
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
 
public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       this.sendOrderedBroadcast(new Intent(
              "android.provier.zhongnan.broadcast"), null);
    }
}


AndroidManifest.xml文件:

 

 

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     package="com.zhongnan.bc" android:versionCode="1" android:versionName="1.0">  
  3.     <uses-sdk android:minSdkVersion="8" />  
  4.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  5.        <activity android:name=".MainActivity" android:label="@string/app_name">  
  6.            <intent-filter>  
  7.               <action android:name="android.intent.action.MAIN" />  
  8.               <category android:name="android.intent.category.LAUNCHER" />  
  9.            </intent-filter>  
  10.        </activity>  
  11.        <receiver android:name=".BroadcastTest1">  
  12.            <intent-filter>  
  13.               <action android:name="android.provier.zhongnan.broadcast"  
  14.                   android:priority="400" />  
  15.            </intent-filter>  
  16.        </receiver>  
  17.        <receiver android:name=".BroadcastTest2">  
  18.            <intent-filter>  
  19.               <action android:name="android.provier.zhongnan.broadcast"  
  20.                   android:priority="500" />  
  21.            </intent-filter>  
  22.        </receiver>  
  23.     </application>  
  24. </manifest>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zhongnan.bc" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
       <activity android:name=".MainActivity" android:label="@string/app_name">
           <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
       <receiver android:name=".BroadcastTest1">
           <intent-filter>
              <action android:name="android.provier.zhongnan.broadcast"
                  android:priority="400" />
           </intent-filter>
       </receiver>
       <receiver android:name=".BroadcastTest2">
           <intent-filter>
              <action android:name="android.provier.zhongnan.broadcast"
                  android:priority="500" />
           </intent-filter>
       </receiver>
    </application>
</manifest>

 

 

在MainActivity中发送有序广播Context.sendOrderedBroadcast(),由于BroadcastTest2中设置的接收优先级比较高,所以在BroadcastTest2中将首先接收到广播意图,可以在BroadcastTest2中对该广播意图进行处理,可以加入处理后的数据给后面的接收器使用,也可以在该接收器中终止广播的进一步传递。在广播中加入处理后的数据使用setResultExtras(Bundle bundle)方法,关于Bundle类,类似于HashMap,不熟悉的朋友可以参考文档,或者查看我的另一篇博客。在后面的接收器中使用getResultExtras(boolean flag)接收前面的接收器存放的数据,其中的boolean参数含义为:true代表如果前面的接收器没有存放数据,则自动创建一个空的Bundle对象,false则表示如果前面的接收器如果没有存放任何数据则返回null。

 

广播接收器中权限的定义:

    在发送广播时,无论是无序广播(Normal Broadcast)还是有序广播(Ordered Broadcast)都有类似的方法:sendBroadcast (Intent intent, String receiverPermission), sendOrderedBroadcast (Intent intent, String receiverPermission)。其中第二个参数是设置权限,即接收器必须具有相应的权限才能正常接收到广播。

 

下面是在上述例子的基础上添加自定义权限的例子:

         接收器1:

  1. import android.content.BroadcastReceiver;  
  2. import android.content.Context;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5.    
  6. public class BroadcastTest2 extends BroadcastReceiver {  
  7.    
  8.     @Override  
  9.     public void onReceive(Context arg0, Intent arg1) {  
  10.        System.out.println("运行线程:    " + Thread.currentThread().getId() + "   "  
  11.               + Thread.currentThread().getName());  
  12.        System.out.println("广播意图的动作:  " + arg1.getAction());  
  13.        Bundle bundle = new Bundle();  
  14.        bundle.putString("test""zhongnan");  
  15.        setResultExtras(bundle);  
  16.     }  
  17. }  
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
 
public class BroadcastTest2 extends BroadcastReceiver {
 
    @Override
    public void onReceive(Context arg0, Intent arg1) {
       System.out.println("运行线程:    " + Thread.currentThread().getId() + "   "
              + Thread.currentThread().getName());
       System.out.println("广播意图的动作:  " + arg1.getAction());
       Bundle bundle = new Bundle();
       bundle.putString("test", "zhongnan");
       setResultExtras(bundle);
    }
}


接收器2

 

  1. import android.content.BroadcastReceiver;  
  2. import android.content.Context;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5.    
  6. public class BroadcastTest1 extends BroadcastReceiver {  
  7.    
  8.     @Override  
  9.     public void onReceive(Context arg0, Intent arg1) {  
  10.        System.out.println("运行线程:    " + Thread.currentThread().getId() + "   "  
  11.               + Thread.currentThread().getName());  
  12.        System.out.println("广播意图的动作:  " + arg1.getAction());  
  13.        Bundle bundle = getResultExtras(false);  
  14.         
  15.        if (bundle == null) {  
  16.            System.out.println("没有得到上次传递的数据");  
  17.        } else {  
  18.            System.out.println("测试:  " + bundle.getString("test"));  
  19.        }  
  20.     }  
  21. }  
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
 
public class BroadcastTest1 extends BroadcastReceiver {
 
    @Override
    public void onReceive(Context arg0, Intent arg1) {
       System.out.println("运行线程:    " + Thread.currentThread().getId() + "   "
              + Thread.currentThread().getName());
       System.out.println("广播意图的动作:  " + arg1.getAction());
       Bundle bundle = getResultExtras(false);
      
       if (bundle == null) {
           System.out.println("没有得到上次传递的数据");
       } else {
           System.out.println("测试:  " + bundle.getString("test"));
       }
    }
}


Activity,用来发送广播:

 

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

 

public class MainActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

       this.sendOrderedBroadcast(new Intent(

              "android.provier.zhongnan.broadcast"), "xzq.zhongnan.test");

    }

}

 

代码中与上述例子最大的差别在于MainActivity中发送广播的代码: this.sendOrderedBroadcast(new Intent(

              "android.provier.zhongnan.broadcast"), "xzq.zhongnan.test")增加了自定义的一个权限。

在AndroidManifest文件中配置自定义的权限:

    <permission android:protectionLevel="normal" android:name="xzq.zhongnan.test"></permission>

关于如何在工程中自定义权限请查阅相关文档,或查看我的另一篇博客。

相应的,接收器中必须设置接收权限:

<uses-permission android:name="xzq.zhongnan.test"></uses-permission>

这样,接收器就可以正确接收到广播了。

 

 

 

 

另外,上述程序已讲到,BroadcastReceiver是允许在主线程中的,所以,在onReceive方法中执行的代码,运行时间不能超过5s,否则将报出程序没有相应的异常,如果要执行的代码运行的时间比较长,可以使用Service组件。

posted @ 2013-03-29 07:54  青城幻影  阅读(453)  评论(0编辑  收藏  举报