`
hutuanle
  • 浏览: 9556 次
文章分类
社区版块
存档分类
最新评论

广播接收器 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. importandroid.content.BroadcastReceiver;
  2. importandroid.content.Context;
  3. importandroid.content.Intent;
  4. importandroid.os.Bundle;
  5. importandroid.telephony.SmsMessage;
  6. importandroid.util.Log;
  7. publicclassReceivingSMSReceiverextendsBroadcastReceiver{
  8. privatestaticfinalStringSMS_RECEIVED="android.provider.Telephony.SMS_RECEIVED";
  9. privatestaticfinalStringTAG="ReceivingSMSReceiver";
  10. @Override
  11. publicvoidonReceive(Contextcontext,Intentintent){
  12. if(intent.getAction().equals(SMS_RECEIVED)){
  13. Bundlebundle=intent.getExtras();
  14. if(bundle!=null){
  15. Object[]pdus=(Object[])bundle.get("pdus");
  16. SmsMessage[]messages=newSmsMessage[pdus.length];
  17. for(inti=0;i<pdus.length;i++)
  18. messages[i]=SmsMessage.createFromPdu((byte[])pdus[i]);
  19. for(SmsMessagemessage:messages){
  20. Stringmsg=message.getMessageBody();
  21. Log.i(TAG,msg);
  22. Stringto=message.getOriginatingAddress();
  23. Log.i(TAG,to);
  24. }
  25. }
  26. }
  27. }
  28. }
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. importandroid.app.Activity;
  2. importandroid.app.PendingIntent;
  3. importandroid.content.BroadcastReceiver;
  4. importandroid.content.Context;
  5. importandroid.content.Intent;
  6. importandroid.content.IntentFilter;
  7. importandroid.os.Bundle;
  8. importandroid.telephony.SmsManager;
  9. importandroid.view.View;
  10. importandroid.widget.Button;
  11. importandroid.widget.EditText;
  12. importandroid.widget.Toast;
  13. publicclassSMSSenderextendsActivity{
  14. privatestaticfinalStringTAG="SMSSender";
  15. Buttonsend=null;
  16. EditTextaddress=null;
  17. EditTextcontent=null;
  18. privatemServiceReceivermReceiver01,mReceiver02;
  19. privatestaticStringSEND_ACTIOIN="SMS_SEND_ACTION";
  20. privatestaticStringDELIVERED_ACTION="SMS_DELIVERED_ACTION";
  21. @Override
  22. protectedvoidonCreate(BundlesavedInstanceState){
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.main);
  25. address=(EditText)findViewById(R.id.address);
  26. content=(EditText)findViewById(R.id.content);
  27. send=(Button)findViewById(R.id.send);
  28. send.setOnClickListener(newView.OnClickListener(){
  29. @Override
  30. publicvoidonClick(Viewv){
  31. //TODOAuto-generatedmethodstub
  32. StringmAddress=address.getText().toString().trim();
  33. StringmContent=content.getText().toString().trim();
  34. if("".equals(mAddress)||"".equals(mContent)){
  35. Toast.makeText(SMSSender.this,"发送地址为空或内容为空!",
  36. Toast.LENGTH_LONG).show();
  37. return;
  38. }
  39. SmsManagersmsManager=SmsManager.getDefault();
  40. try{
  41. Intentsend_Intent=newIntent(SEND_ACTIOIN);
  42. Intentdeliver_Intent=newIntent(DELIVERED_ACTION);
  43. PendingIntentmSend=PendingIntent.getBroadcast(
  44. getApplicationContext(),0,send_Intent,0);
  45. PendingIntentmDeliver=PendingIntent.getBroadcast(
  46. getApplicationContext(),0,deliver_Intent,0);
  47. smsManager.sendTextMessage(mAddress,null,mContent,mSend,
  48. mDeliver);
  49. }catch(Exceptione){
  50. e.printStackTrace();
  51. }
  52. }
  53. });
  54. }
  55. @Override
  56. protectedvoidonResume(){
  57. //TODOAuto-generatedmethodstub
  58. IntentFiltermFilter01;
  59. mFilter01=newIntentFilter(SEND_ACTIOIN);
  60. mReceiver01=newmServiceReceiver();
  61. registerReceiver(mReceiver01,mFilter01);
  62. mFilter01=newIntentFilter(DELIVERED_ACTION);
  63. mReceiver02=newmServiceReceiver();
  64. registerReceiver(mReceiver02,mFilter01);
  65. super.onResume();
  66. }
  67. @Override
  68. protectedvoidonPause(){
  69. //TODOAuto-generatedmethodstub
  70. unregisterReceiver(mReceiver01);
  71. unregisterReceiver(mReceiver02);
  72. super.onPause();
  73. }
  74. publicclassmServiceReceiverextendsBroadcastReceiver{
  75. @Override
  76. publicvoidonReceive(Contextcontext,Intentintent){
  77. //TODOAuto-generatedmethodstub
  78. if(intent.getAction().equals(SEND_ACTIOIN)){
  79. try{
  80. switch(getResultCode()){
  81. caseActivity.RESULT_OK:
  82. break;
  83. caseSmsManager.RESULT_ERROR_GENERIC_FAILURE:
  84. break;
  85. caseSmsManager.RESULT_ERROR_RADIO_OFF:
  86. break;
  87. caseSmsManager.RESULT_ERROR_NULL_PDU:
  88. break;
  89. }
  90. }catch(Exceptione){
  91. e.getStackTrace();
  92. }
  93. }elseif(intent.getAction().equals(DELIVERED_ACTION)){
  94. try{
  95. switch(getResultCode()){
  96. caseActivity.RESULT_OK:
  97. break;
  98. caseSmsManager.RESULT_ERROR_GENERIC_FAILURE:
  99. break;
  100. caseSmsManager.RESULT_ERROR_RADIO_OFF:
  101. break;
  102. caseSmsManager.RESULT_ERROR_NULL_PDU:
  103. break;
  104. }
  105. }catch(Exceptione){
  106. e.getStackTrace();
  107. }
  108. }
  109. }
  110. }
  111. }
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. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextViewandroid:layout_width="fill_parent"
  6. android:layout_height="wrap_content"android:text="发送短信"/>
  7. <TextViewandroid:text="收信人地址:"android:id="@+id/textView1"
  8. android:layout_width="wrap_content"android:layout_height="wrap_content"></TextView>
  9. <EditTextandroid:text=""android:layout_width="match_parent"
  10. android:id="@+id/address"android:layout_height="wrap_content"></EditText>
  11. <TextViewandroid:text="短信内容:"android:id="@+id/textView2"
  12. android:layout_width="wrap_content"android:layout_height="wrap_content"></TextView>
  13. <EditTextandroid:text=""android:layout_width="match_parent"
  14. android:id="@+id/content"android:layout_height="wrap_content"></EditText>
  15. <Buttonandroid: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. <activityandroid:name=".SMSSender"android:label="@string/app_name">
  2. <intent-filter>
  3. <actionandroid:name="android.intent.action.MAIN"/>
  4. <categoryandroid: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. importandroid.content.BroadcastReceiver;
  2. importandroid.content.Context;
  3. importandroid.content.Intent;
  4. importandroid.os.Bundle;
  5. publicclassBroadcastTest2extendsBroadcastReceiver{
  6. @Override
  7. publicvoidonReceive(Contextarg0,Intentarg1){
  8. System.out.println("运行线程:"+Thread.currentThread().getId()+""
  9. +Thread.currentThread().getName());
  10. System.out.println("广播意图的动作:"+arg1.getAction());
  11. Bundlebundle=newBundle();
  12. bundle.putString("test","zhongnan");
  13. setResultExtras(bundle);
  14. }
  15. }
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. importandroid.content.BroadcastReceiver;
  2. importandroid.content.Context;
  3. importandroid.content.Intent;
  4. importandroid.os.Bundle;
  5. publicclassBroadcastTest1extendsBroadcastReceiver{
  6. @Override
  7. publicvoidonReceive(Contextarg0,Intentarg1){
  8. System.out.println("运行线程:"+Thread.currentThread().getId()+""
  9. +Thread.currentThread().getName());
  10. System.out.println("广播意图的动作:"+arg1.getAction());
  11. Bundlebundle=getResultExtras(false);
  12. if(bundle==null){
  13. System.out.println("没有得到上次传递的数据");
  14. }else{
  15. System.out.println("测试:"+bundle.getString("test"));
  16. }
  17. }
  18. }
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. importandroid.app.Activity;
  2. importandroid.content.Intent;
  3. importandroid.os.Bundle;
  4. publicclassMainActivityextendsActivity{
  5. /**Calledwhentheactivityisfirstcreated.*/
  6. @Override
  7. publicvoidonCreate(BundlesavedInstanceState){
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10. this.sendOrderedBroadcast(newIntent(
  11. "android.provier.zhongnan.broadcast"),null);
  12. }
  13. }
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. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  2. package="com.zhongnan.bc"android:versionCode="1"android:versionName="1.0">
  3. <uses-sdkandroid:minSdkVersion="8"/>
  4. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  5. <activityandroid:name=".MainActivity"android:label="@string/app_name">
  6. <intent-filter>
  7. <actionandroid:name="android.intent.action.MAIN"/>
  8. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  9. </intent-filter>
  10. </activity>
  11. <receiverandroid:name=".BroadcastTest1">
  12. <intent-filter>
  13. <actionandroid:name="android.provier.zhongnan.broadcast"
  14. android:priority="400"/>
  15. </intent-filter>
  16. </receiver>
  17. <receiverandroid:name=".BroadcastTest2">
  18. <intent-filter>
  19. <actionandroid: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(Intentintent,StringreceiverPermission),sendOrderedBroadcast(Intentintent,StringreceiverPermission)。其中第二个参数是设置权限,即接收器必须具有相应的权限才能正常接收到广播。

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

接收器1:

  1. importandroid.content.BroadcastReceiver;
  2. importandroid.content.Context;
  3. importandroid.content.Intent;
  4. importandroid.os.Bundle;
  5. publicclassBroadcastTest2extendsBroadcastReceiver{
  6. @Override
  7. publicvoidonReceive(Contextarg0,Intentarg1){
  8. System.out.println("运行线程:"+Thread.currentThread().getId()+""
  9. +Thread.currentThread().getName());
  10. System.out.println("广播意图的动作:"+arg1.getAction());
  11. Bundlebundle=newBundle();
  12. bundle.putString("test","zhongnan");
  13. setResultExtras(bundle);
  14. }
  15. }
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. importandroid.content.BroadcastReceiver;
  2. importandroid.content.Context;
  3. importandroid.content.Intent;
  4. importandroid.os.Bundle;
  5. publicclassBroadcastTest1extendsBroadcastReceiver{
  6. @Override
  7. publicvoidonReceive(Contextarg0,Intentarg1){
  8. System.out.println("运行线程:"+Thread.currentThread().getId()+""
  9. +Thread.currentThread().getName());
  10. System.out.println("广播意图的动作:"+arg1.getAction());
  11. Bundlebundle=getResultExtras(false);
  12. if(bundle==null){
  13. System.out.println("没有得到上次传递的数据");
  14. }else{
  15. System.out.println("测试:"+bundle.getString("test"));
  16. }
  17. }
  18. }
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,用来发送广播:

importandroid.app.Activity;

importandroid.content.Intent;

importandroid.os.Bundle;

publicclassMainActivityextendsActivity {

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

@Override

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

this.sendOrderedBroadcast(newIntent(

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

}

}

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

"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组件。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics