- 浏览: 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(用于发送和接收短信):
- importandroid.content.BroadcastReceiver;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- importandroid.telephony.SmsMessage;
- importandroid.util.Log;
- publicclassReceivingSMSReceiverextendsBroadcastReceiver{
- privatestaticfinalStringSMS_RECEIVED="android.provider.Telephony.SMS_RECEIVED";
- privatestaticfinalStringTAG="ReceivingSMSReceiver";
- @Override
- publicvoidonReceive(Contextcontext,Intentintent){
- if(intent.getAction().equals(SMS_RECEIVED)){
- Bundlebundle=intent.getExtras();
- if(bundle!=null){
- Object[]pdus=(Object[])bundle.get("pdus");
- SmsMessage[]messages=newSmsMessage[pdus.length];
- for(inti=0;i<pdus.length;i++)
- messages[i]=SmsMessage.createFromPdu((byte[])pdus[i]);
- for(SmsMessagemessage:messages){
- Stringmsg=message.getMessageBody();
- Log.i(TAG,msg);
- Stringto=message.getOriginatingAddress();
- Log.i(TAG,to);
- }
- }
- }
- }
- }
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用来发送短信:
- importandroid.app.Activity;
- importandroid.app.PendingIntent;
- importandroid.content.BroadcastReceiver;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.content.IntentFilter;
- importandroid.os.Bundle;
- importandroid.telephony.SmsManager;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.EditText;
- importandroid.widget.Toast;
- publicclassSMSSenderextendsActivity{
- privatestaticfinalStringTAG="SMSSender";
- Buttonsend=null;
- EditTextaddress=null;
- EditTextcontent=null;
- privatemServiceReceivermReceiver01,mReceiver02;
- privatestaticStringSEND_ACTIOIN="SMS_SEND_ACTION";
- privatestaticStringDELIVERED_ACTION="SMS_DELIVERED_ACTION";
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- 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(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- StringmAddress=address.getText().toString().trim();
- StringmContent=content.getText().toString().trim();
- if("".equals(mAddress)||"".equals(mContent)){
- Toast.makeText(SMSSender.this,"发送地址为空或内容为空!",
- Toast.LENGTH_LONG).show();
- return;
- }
- SmsManagersmsManager=SmsManager.getDefault();
- try{
- Intentsend_Intent=newIntent(SEND_ACTIOIN);
- Intentdeliver_Intent=newIntent(DELIVERED_ACTION);
- PendingIntentmSend=PendingIntent.getBroadcast(
- getApplicationContext(),0,send_Intent,0);
- PendingIntentmDeliver=PendingIntent.getBroadcast(
- getApplicationContext(),0,deliver_Intent,0);
- smsManager.sendTextMessage(mAddress,null,mContent,mSend,
- mDeliver);
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- });
- }
- @Override
- protectedvoidonResume(){
- //TODOAuto-generatedmethodstub
- IntentFiltermFilter01;
- mFilter01=newIntentFilter(SEND_ACTIOIN);
- mReceiver01=newmServiceReceiver();
- registerReceiver(mReceiver01,mFilter01);
- mFilter01=newIntentFilter(DELIVERED_ACTION);
- mReceiver02=newmServiceReceiver();
- registerReceiver(mReceiver02,mFilter01);
- super.onResume();
- }
- @Override
- protectedvoidonPause(){
- //TODOAuto-generatedmethodstub
- unregisterReceiver(mReceiver01);
- unregisterReceiver(mReceiver02);
- super.onPause();
- }
- publicclassmServiceReceiverextendsBroadcastReceiver{
- @Override
- publicvoidonReceive(Contextcontext,Intentintent){
- //TODOAuto-generatedmethodstub
- if(intent.getAction().equals(SEND_ACTIOIN)){
- try{
- switch(getResultCode()){
- caseActivity.RESULT_OK:
- break;
- caseSmsManager.RESULT_ERROR_GENERIC_FAILURE:
- break;
- caseSmsManager.RESULT_ERROR_RADIO_OFF:
- break;
- caseSmsManager.RESULT_ERROR_NULL_PDU:
- break;
- }
- }catch(Exceptione){
- e.getStackTrace();
- }
- }elseif(intent.getAction().equals(DELIVERED_ACTION)){
- try{
- switch(getResultCode()){
- caseActivity.RESULT_OK:
- break;
- caseSmsManager.RESULT_ERROR_GENERIC_FAILURE:
- break;
- caseSmsManager.RESULT_ERROR_RADIO_OFF:
- break;
- caseSmsManager.RESULT_ERROR_NULL_PDU:
- break;
- }
- }catch(Exceptione){
- e.getStackTrace();
- }
- }
- }
- }
- }
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(); } } } } }
界面布局:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextViewandroid:layout_width="fill_parent"
- android:layout_height="wrap_content"android:text="发送短信"/>
- <TextViewandroid:text="收信人地址:"android:id="@+id/textView1"
- android:layout_width="wrap_content"android:layout_height="wrap_content"></TextView>
- <EditTextandroid:text=""android:layout_width="match_parent"
- android:id="@+id/address"android:layout_height="wrap_content"></EditText>
- <TextViewandroid:text="短信内容:"android:id="@+id/textView2"
- android:layout_width="wrap_content"android:layout_height="wrap_content"></TextView>
- <EditTextandroid:text=""android:layout_width="match_parent"
- android:id="@+id/content"android:layout_height="wrap_content"></EditText>
- <Buttonandroid:text="发送"android:id="@+id/send"
- android:layout_width="wrap_content"android:layout_height="wrap_content"></Button>
- </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的配置:
- <activityandroid:name=".SMSSender"android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </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:
- importandroid.content.BroadcastReceiver;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- publicclassBroadcastTest2extendsBroadcastReceiver{
- @Override
- publicvoidonReceive(Contextarg0,Intentarg1){
- System.out.println("运行线程:"+Thread.currentThread().getId()+""
- +Thread.currentThread().getName());
- System.out.println("广播意图的动作:"+arg1.getAction());
- Bundlebundle=newBundle();
- bundle.putString("test","zhongnan");
- setResultExtras(bundle);
- }
- }
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:
- importandroid.content.BroadcastReceiver;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- publicclassBroadcastTest1extendsBroadcastReceiver{
- @Override
- publicvoidonReceive(Contextarg0,Intentarg1){
- System.out.println("运行线程:"+Thread.currentThread().getId()+""
- +Thread.currentThread().getName());
- System.out.println("广播意图的动作:"+arg1.getAction());
- Bundlebundle=getResultExtras(false);
- if(bundle==null){
- System.out.println("没有得到上次传递的数据");
- }else{
- System.out.println("测试:"+bundle.getString("test"));
- }
- }
- }
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{
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this.sendOrderedBroadcast(newIntent(
- "android.provier.zhongnan.broadcast"),null);
- }
- }
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文件:
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.zhongnan.bc"android:versionCode="1"android:versionName="1.0">
- <uses-sdkandroid:minSdkVersion="8"/>
- <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
- <activityandroid:name=".MainActivity"android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- <receiverandroid:name=".BroadcastTest1">
- <intent-filter>
- <actionandroid:name="android.provier.zhongnan.broadcast"
- android:priority="400"/>
- </intent-filter>
- </receiver>
- <receiverandroid:name=".BroadcastTest2">
- <intent-filter>
- <actionandroid:name="android.provier.zhongnan.broadcast"
- android:priority="500"/>
- </intent-filter>
- </receiver>
- </application>
- </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:
- importandroid.content.BroadcastReceiver;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- publicclassBroadcastTest2extendsBroadcastReceiver{
- @Override
- publicvoidonReceive(Contextarg0,Intentarg1){
- System.out.println("运行线程:"+Thread.currentThread().getId()+""
- +Thread.currentThread().getName());
- System.out.println("广播意图的动作:"+arg1.getAction());
- Bundlebundle=newBundle();
- bundle.putString("test","zhongnan");
- setResultExtras(bundle);
- }
- }
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:
- importandroid.content.BroadcastReceiver;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- publicclassBroadcastTest1extendsBroadcastReceiver{
- @Override
- publicvoidonReceive(Contextarg0,Intentarg1){
- System.out.println("运行线程:"+Thread.currentThread().getId()+""
- +Thread.currentThread().getName());
- System.out.println("广播意图的动作:"+arg1.getAction());
- Bundlebundle=getResultExtras(false);
- if(bundle==null){
- System.out.println("没有得到上次传递的数据");
- }else{
- System.out.println("测试:"+bundle.getString("test"));
- }
- }
- }
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组件。
相关推荐
Android 广播接收 发送 Broadcast demo
broadcast_Android 广播机制broadcast用法实例.rar
Android Broadcast 广播机制 实例
【实验要求】 1、 练习使用静态方法和动态方法注册广播接收器 2、 练习发送广播消息的方法 3、 完成实验报告 二、实验内容 1、 新建 Android 应用程序项目 BroadcastTest; 2、 业务逻辑代码与界面布局文件如下: 1)...
参考博客:https://blog.csdn.net/Da_Xiong000/article/details/105326901
自定义广播,有序广播,广播接收器 的demo,亲测好用,便宜拿去
主要介绍了Android中的广播(BroadCast)详细介绍,本文讲解了什么是广播、广播有什么用、实现广播、动态注册方式、配置文件方式等内容,需要的朋友可以参考下
第八章_Android广播事件处理Broadcast_Receiver 让你更快了解
一个简单的Android广播接收器库。 介绍 该库旨在使用“观察者”设计模式处理所有广播接收器操作,以保持更新所有广播侦听器。 例如,通过使用此库,无论您在应用程序中的哪个位置,都可以跟踪应用程序网络状态。 只...
cineio-broadcast-android, cine.io Android广播 SDK cine.io sdk 用于 cine.io 广播的android库 。这个库在 Android 5.0 + ( Lollipop ) 上不稳定。 对由此带来的任何不便,我们深表歉意. 在我们做之前,我们喜欢...
Android利用广播BroadCast监听网络的变化。该Demo是网络资源整理,可以正常运行。你可以看到内部接口的使用,广播机制的使用,网络连接状态的检查等。
Android中采用广播接收者实现短信监控器、拦截外拨电话功能案例!
Android自学笔记-17-广播接收者(BroadcastReceiver)
Broadcast广播的使用,简单介绍了静态、动态注册广播,以及接受系统发出的广播和自定义本地广播
Android系统中的广播(Broadcast)机制简要介绍和学习计划
Broadcast_Reciever 简单系统广播接收器示例如何使用动态广播接收器什么是广播接收器? Android应用程序可以从Android系统本身或其他android应用程序发送和接收广播消息。这些广播是在发生紧急事件时发送的。 / n一...
Androidbroadcast 广播接收器
1.接受和发送UDP信号 2.ListView显示收到的UDP DATA 3.能够接受到本机内容
Android系统中的广播Broadcast,Receiver例子,可以用LogCat察看整个运行的生命周期.MainActivity界面上有两个按钮,分别是Start Counter和Stop Counter按钮,点击前者开始计数,而点击后者则停止计数。
Android有序广播(Ordered Broadcast)的实现:接受者按声明的优先级依次接收广播