1.Android N å大ç»ä»¶çå·¥ä½åç
2.android broadreceiver è¿è¡å¨åªä¸ªè¿ç¨
3.在线等啊,有人出过这个问题没有 java.lang.IllegalStateException: Broadcast already finished
Android N å大ç»ä»¶çå·¥ä½åç
æ¬æ侧é讲解android N ç³»ç»ä¸å大ç»ä»¶çå·¥ä½åçï¼ä¸åç³»ç»åçç¥æå·®å«ãéè¿åæå大ç»ä»¶çå·¥ä½æµç¨å 深对Android Frameworkçç解ï¼ä¹ä¸ºæ件åå¼åæä¸åºç¡ã
Activity
å±ç¤ºä¸ä¸ªçé¢å¹¶åç¨æ·äº¤äºï¼å®æ®æ¼çæ¯ä¸ä¸ªåå°çé¢çè§è²ã
Service
计ç®åç»ä»¶ï¼ç¨äºåå°æ§è¡ä¸ç³»å计ç®ä»»å¡ï¼å·¥ä½å¨ä¸»çº¿ç¨ï¼èæ¶æä½éè¦å¦èµ·çº¿ç¨ï¼ å为å¯å¨ç¶æåç»å®ç¶æã
BroadcastReceiver
æ¶æ¯åç»ä»¶ï¼ä¸»è¦ç¨äºä¸åç»ä»¶æè ä¸ååºç¨ä¹é´çæ¶æ¯ä¼ éï¼å®å·¥ä½å¨ç³»ç»å é¨ï¼ä¸éåæ§è¡èæ¶æä½ï¼æä½è¶ è¿5sï¼ä¼åºç°ANRã
ContentProvider
æ°æ®å ±äº«åç»ä»¶ï¼ç¨äºåå ¶ä»ç»ä»¶æè åºç¨å ±äº«æ°æ®ï¼ä¸»è¦æ§è¡CURDæä½ã
æ们å¯å¨ä¸ä¸ªactivityæ两ç§æ¹æ³ï¼
第ä¸ç§ï¼Activityç´æ¥å¯å¨æ¹å¼ï¼ï¼
Intent intent = new Intent(this,引爆点源码公式 MainActivity.class);
startActivity(intent);
第äºç§ï¼Contextå¯å¨æ¹å¼ï¼
Intent intent = new Intent(this, MainActivity.class);
getApplicationContext().startActivity(intent);
ä¸åçå¯å¨æ¹å¼Activityçå·¥ä½æµç¨æç¹å·®å«ã
两ç§å¯å¨é½ä¼è°ç¨å°Instrumentationç±»ä¸çexecStartActivityçæ¹æ³ï¼ç³»ç»æç»æ¯éè¿ActivityThreadä¸çperformLaunchActivityå®æActivityçå建åå¯å¨ã
performLaunchActivityæ¹æ³ä¸»è¦å®æ以ä¸å·¥ä½ï¼
1ãéè¿ActivityClientRecord对象è·åå¯å¨activityçç»ä»¶ä¿¡æ¯
2ãéè¿mInstrumentation对象çnewActivityæ¹æ³è°ç¨classloaderå®æactivityçå建
3ãéè¿r.packageInfo(LoadedApk 对象)çmakeApplicationæ¹æ³å°è¯å建Application对象
4ãå建ContextImpl对象并è°ç¨Activityçattachæ¹æ³å®æä¸äºæ°æ®çåå§å
5ãè°ç¨ActivityçonCreateæ¹æ³
å¨Activityå¯å¨çè¿ç¨ä¸ï¼Appè¿ç¨ä¼é¢ç¹å°ä¸AMSè¿ç¨è¿è¡éä¿¡ï¼
Appè¿ç¨ä¼å§æAMSè¿ç¨å®æActivityçå½å¨æç管ç以åä»»å¡æ ç管çï¼è¿ä¸ªéä¿¡è¿ç¨AMSæ¯Server端ï¼Appè¿ç¨éè¿ææAMSçclient代çIActivityManagerå®æéä¿¡è¿ç¨ï¼
AMSè¿ç¨å®æçå½å¨æ管ç以åä»»å¡æ 管çåï¼ä¼ææ§å¶æ交ç»Appè¿ç¨ï¼è®©Appè¿ç¨å®æActivity类对象çå建ï¼ä»¥åçå½å¨æåè°ï¼è¿ä¸ªéä¿¡è¿ç¨ä¹æ¯éè¿Binderå®æçï¼Appæå¨server端çBinder对象åå¨äºActivityThreadçå é¨ç±»ApplicationThreadï¼AMSæå¨clientéè¿ææIApplicationThreadç代ç对象å®æ对äºAppè¿ç¨çéä¿¡ã
Serviceæ两ç§å¯å¨æ¹å¼ï¼startService()åbindService()ï¼ä¸¤ç§ç¶æå¯ä»¥å¹¶å:
startServiceæµç¨
bindServiceæµç¨
BroadcastReceiverçå·¥ä½è¿ç¨ä¸»è¦å æ¬å¹¿æç注åãåéåæ¥æ¶:
å¨æ注åè¿ç¨ï¼
åéè¿ç¨
éæ注åæ¯ç±PackageManagerServiceï¼PMSï¼å¨åºç¨å®è£ çæ¶åå®ææ´ä¸ªæ³¨åè¿ç¨çï¼é¤å¹¿æ以å¤ï¼å ¶ä»ä¸å¤§ç»ä»¶ä¹é½æ¯å¨åºç¨å®è£ æ¶ç±PMS解æ并注åçã
æ¯ä¸ªè¿ç¨çå ¥å£é½æ¯ActivityThead.main()ï¼Appçå¯å¨æµç¨å¦ä¸ï¼
ä»æºç ä¸å¯ä»¥çåºï¼
åºç¨å¯å¨çå ¥å£ä¸ºActivityThreadçmainæ¹æ³ï¼mainæ¹æ³ä¼å建ActivityThreadå®ä¾å¹¶å建主线ç¨æ¶æ¯éåã
attachæ¹æ³ä¸è¿ç¨è°ç¨AMSçattachApplicationæ¹æ³ï¼å¹¶æä¾ApplicationThreadç¨äºåAMSçéä¿¡ã
attachApplicationæ¹æ³ä¼éè¿bindApplicationæ¹æ³åHæ¥è°åActivityThreadçhandleBindApplicationï¼è¿ä¸ªæ¹æ³ä¼å å建Applicationï¼åå è½½ContentProviderï¼ç¶åæä¼åè°ApplicationçonCreateæ¹æ³ã
ç±ä¸å¾å¯ä»¥çåºï¼å¨ContentProviderçå¯å¨è¿ç¨ä¸ä¼´éçappè¿ç¨çå¯å¨ã
ContentProviderçå ¶ä»CURDæä½å¦insertï¼deleteï¼updateè·queryçæµç¨ç±»ä¼¼ã
android broadreceiver è¿è¡å¨åªä¸ªè¿ç¨
广ææ¥æ¶å¨æ¯è¿è¡å¨UI线ç¨ãä¸é¢ä»ç»æ¥èªäºandroidå¦ä¹ æåï¼androidå¦ä¹ æåå å«9ä¸ªç« èï¼ä¸ªä¾åï¼æºç ææ¡£é便çï¼ä¾åé½æ¯å¯äº¤äºï¼å¯è¿è¡ï¼æºç éç¨android studioç®å½ç»æï¼é«äº®æ¾ç¤ºä»£ç ï¼ææ¡£é½éç¨ææ¡£ç»æå¾æ¾ç¤ºï¼å¯ä»¥å¿«éå®ä½ãææºå©æä¸ä¸è½½,å¾æ ä¸æè´å£³
1.Android广ææºå¶æ¦è¿°
Android广æå为两个æ¹é¢ï¼å¹¿æåéè å广ææ¥æ¶è ï¼é常æ åµä¸ï¼BroadcastReceiveræçå°±æ¯å¹¿ææ¥æ¶è ï¼å¹¿ææ¥æ¶å¨ï¼ã广æä½ä¸ºAndroidç»ä»¶é´çéä¿¡æ¹å¼ï¼å¯ä»¥ä½¿ç¨çåºæ¯å¦ä¸ï¼
1.åä¸appå é¨çåä¸ç»ä»¶å çæ¶æ¯éä¿¡ï¼å个æå¤ä¸ªçº¿ç¨ä¹é´ï¼ï¼
2.åä¸appå é¨çä¸åç»ä»¶ä¹é´çæ¶æ¯éä¿¡ï¼å个è¿ç¨ï¼ï¼
3.åä¸appå ·æå¤ä¸ªè¿ç¨çä¸åç»ä»¶ä¹é´çæ¶æ¯éä¿¡ï¼
4.ä¸åappä¹é´çç»ä»¶ä¹é´æ¶æ¯éä¿¡ï¼
5.Androidç³»ç»å¨ç¹å®æ åµä¸ä¸Appä¹é´çæ¶æ¯éä¿¡ã
ä»å®ç°åççä¸ï¼Androidä¸ç广æ使ç¨äºè§å¯è 模å¼ï¼åºäºæ¶æ¯çåå¸/订é äºä»¶æ¨¡åãå æ¤ï¼ä»å®ç°çè§åº¦æ¥çï¼Androidä¸ç广æå°å¹¿æçåéè åæ¥åè æ大ç¨åº¦ä¸è§£è¦ï¼ä½¿å¾ç³»ç»è½å¤æ¹ä¾¿éæï¼æ´ææ©å±ãå ·ä½å®ç°æµç¨è¦ç¹ç²ç¥æ¦æ¬å¦ä¸ï¼
1.广ææ¥æ¶è BroadcastReceiveréè¿Binderæºå¶åAMS(Activity Manager Service)è¿è¡æ³¨åï¼
2.广æåéè éè¿binderæºå¶åAMSåé广æï¼
3.AMSæ¥æ¾ç¬¦åç¸åºæ¡ä»¶ï¼IntentFilter/Permissionçï¼çBroadcastReceiverï¼å°å¹¿æåéå°BroadcastReceiverï¼ä¸è¬æ åµä¸æ¯Activityï¼ç¸åºçæ¶æ¯å¾ªç¯éåä¸ï¼
4.æ¶æ¯å¾ªç¯æ§è¡æ¿å°æ¤å¹¿æï¼åè°BroadcastReceiverä¸çonReceive()æ¹æ³ã
对äºä¸åç广æç±»åï¼ä»¥åä¸åçBroadcastReceiver注åæ¹å¼ï¼å ·ä½å®ç°ä¸ä¼æä¸åãä½æ»ä½æµç¨å¤§è´å¦ä¸ã
ç±æ¤çæ¥ï¼å¹¿æåéè å广ææ¥æ¶è åå«å±äºè§å¯è 模å¼ä¸çæ¶æ¯åå¸å订é 两端ï¼AMSå±äºä¸é´çå¤çä¸å¿ã广æåéè å广ææ¥æ¶è çæ§è¡æ¯å¼æ¥çï¼ååºå»ç广æä¸ä¼å ³å¿ææ æ¥æ¶è æ¥æ¶ï¼ä¹ä¸ç¡®å®æ¥æ¶è å°åºæ¯ä½æ¶æè½æ¥æ¶å°ãæ¾ç¶ï¼æ´ä½æµç¨ä¸EventBusé常类似ã
å¨ä¸æ说å举ç广ææºå¶å ·ä½å¯ä»¥ä½¿ç¨çåºæ¯ä¸ï¼ç°åæå®é åºç¨ä¸çéç¨æ§ï¼
第ä¸ç§æ å½¢ï¼åä¸appå é¨çåä¸ç»ä»¶å çæ¶æ¯éä¿¡ï¼å个æå¤ä¸ªçº¿ç¨ä¹é´ï¼ï¼å®é åºç¨ä¸è¯å®æ¯ä¸ä¼ç¨å°å¹¿ææºå¶çï¼è½ç¶å¯ä»¥ç¨ï¼ï¼æ 论æ¯ä½¿ç¨æ©å±åéä½ç¨åãåºäºæ¥å£çåè°è¿æ¯Handler-post/Handler-Messageçæ¹å¼ï¼é½å¯ä»¥ç´æ¥å¤çæ¤ç±»é®é¢ï¼è¥éç¨å¹¿ææºå¶ï¼æ¾ç¶æäºâæ鸡çåâçæè§ï¼ä¼æ¾å¤ªâéâï¼
第äºç§æ å½¢ï¼åä¸appå é¨çä¸åç»ä»¶ä¹é´çæ¶æ¯éä¿¡ï¼å个è¿ç¨ï¼ï¼å¯¹äºæ¤ç±»éæ±ï¼å¨æäºæå¤æçæ åµä¸å纯çä¾é åºäºæ¥å£çåè°çæ¹å¼ä¸å¥½å¤çï¼æ¤æ¶å¯ä»¥ç´æ¥ä½¿ç¨EventBusçï¼ç¸å¯¹èè¨ï¼EventBusç±äºæ¯é对ç»ä¸è¿ç¨ï¼ç¨äºå¤çæ¤ç±»éæ±é常éåï¼ä¸è½»æ¾è§£è¦ãå¯ä»¥åè§æ件ãAndroidåç»ä»¶/æ§ä»¶é´éä¿¡å©å¨ä¹EventBusãã
第ä¸ãåãäºæ å½¢ï¼ç±äºæ¶åä¸åè¿ç¨é´çæ¶æ¯éä¿¡ï¼æ¤æ¶æ ¹æ®å®é ä¸å¡ä½¿ç¨å¹¿ææºå¶ä¼æ¾å¾é常éå®ãä¸é¢ä¸»è¦é对Android广æä¸çå ·ä½ç¥è¯ç¹è¿è¡æ»ç»ã
2.BroadcastReceiver
èªå®ä¹BroadcastReceiver
èªå®ä¹å¹¿ææ¥æ¶å¨éè¦ç»§æ¿åºç±»BroadcastReceivreï¼å¹¶å®ç°æ½è±¡æ¹æ³onReceive(context, intent)æ¹æ³ã广ææ¥æ¶å¨æ¥æ¶å°ç¸åºå¹¿æåï¼ä¼èªå¨åå°onReceive(..)æ¹æ³ãé»è®¤æ åµä¸ï¼å¹¿ææ¥æ¶å¨ä¹æ¯è¿è¡å¨UI线ç¨ï¼å æ¤ï¼onReceiveæ¹æ³ä¸ä¸è½æ§è¡å¤ªèæ¶çæä½ãå¦åå°å æ¤ANRãä¸è¬æ åµä¸ï¼æ ¹æ®å®é ä¸å¡éæ±ï¼onReceiveæ¹æ³ä¸é½ä¼æ¶åå°ä¸å ¶ä»ç»ä»¶ä¹é´ç交äºï¼å¦åéNotificationãå¯å¨serviceçã
ä¸é¢ä»£ç ç段æ¯ä¸ä¸ªç®åç广ææ¥æ¶å¨çèªå®ä¹ï¼
1 public class MyBroadcastReceiver extends BroadcastReceiver {
2 public static final String TAG = "MyBroadcastReceiver";
3 public static int m = 1;
4
5 @Override
6 public void onReceive(Context context, Intent intent) {
7 Log.w(TAG, "intent:" + intent);
8 String name = intent.getStringExtra("name");
9 Log.w(TAG, "name:" + name + " m=" + m);
m++;
Bundle bundle = intent.getExtras();
}
}
BroadcastReceiver注åç±»å
BroadcastReceiveræ»ä½ä¸å¯ä»¥å为两ç§æ³¨åç±»åï¼éæ注ååå¨æ注åã
1).éæ注åï¼
ç´æ¥å¨AndroidManifest.xmlæ件ä¸è¿è¡æ³¨åãè§åå¦ä¸ï¼
<receiver android:enabled=["true" | "false"]
android:exported=["true" | "false"]
android:icon="drawable resource"
android:label="string resource"
android:name="string"
android:permission="string"
android:process="string" >
. . .
</receiver>
å ¶ä¸ï¼éè¦æ³¨æçå±æ§
android:exported ââæ¤broadcastReceiverè½å¦æ¥æ¶å ¶ä»Appçååºç广æï¼è¿ä¸ªå±æ§é»è®¤å¼æç¹ææï¼å ¶é»è®¤å¼æ¯ç±receiverä¸ææ intent-filterå³å®çï¼å¦ææintent-filterï¼é»è®¤å¼ä¸ºtrueï¼å¦å为falseãï¼åæ ·çï¼activity/serviceä¸çæ¤å±æ§é»è®¤å¼ä¸æ ·éµå¾ªæ¤è§åï¼åæ¶ï¼éè¦æ³¨æçæ¯ï¼è¿ä¸ªå¼ç设å®æ¯ä»¥applicationæè application user id为ççï¼èéè¿ç¨ä¸ºçï¼ä¸ä¸ªåºç¨ä¸å¯è½å«æå¤ä¸ªè¿ç¨ï¼ï¼
android:name ââ æ¤broadcastReceiverç±»åï¼
android:permission ââå¦æ设置ï¼å ·æç¸åºæéç广æåéæ¹åéç广ææè½è¢«æ¤broadcastReceiverææ¥æ¶ï¼
android:process ââbroadcastReceiverè¿è¡æå¤çè¿ç¨ãé»è®¤ä¸ºappçè¿ç¨ãå¯ä»¥æå®ç¬ç«çè¿ç¨ï¼Androidå大åºæ¬ç»ä»¶é½å¯ä»¥éè¿æ¤å±æ§æå®èªå·±çç¬ç«è¿ç¨ï¼
常è§ç注åå½¢å¼æï¼
<receiver android:name=".MyBroadcastReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
å ¶ä¸ï¼intent-filterç±äºæå®æ¤å¹¿ææ¥æ¶å¨å°ç¨äºæ¥æ¶ç¹å®ç广æç±»åãæ¬ç¤ºä¾ä¸ç»åºçæ¯ç¨äºæ¥æ¶ç½ç»ç¶ææ¹åæå¼å¯å¯å¨æ¶ç³»ç»èªèº«æååºç广æãå½æ¤Appé¦æ¬¡å¯å¨æ¶ï¼ç³»ç»ä¼èªå¨å®ä¾åMyBroadcastReceiverï¼å¹¶æ³¨åå°ç³»ç»ä¸ã
ä¹å常说ï¼éæ注åç广ææ¥æ¶å¨å³ä½¿appå·²ç»éåºï¼ä¸»è¦æç¸åºç广æååºï¼ä¾ç¶å¯ä»¥æ¥æ¶å°ï¼ä½æ¤ç§æè¿°èªAndroid 3.1å¼å§æå¯è½ä¸åæç«ï¼å ·ä½åæ详è§æ¬æåé¢é¨åã
2).å¨æ注åï¼
å¨æ注åæ¶ï¼æ é¡»å¨AndroidManifestä¸æ³¨å<receiver/>ç»ä»¶ãç´æ¥å¨ä»£ç ä¸éè¿è°ç¨ContextçregisterReceiverå½æ°ï¼å¯ä»¥å¨ç¨åºä¸å¨æ注åBroadcastReceiverãregisterReceiverçå®ä¹å½¢å¼å¦ä¸ï¼
1 registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
2 registerReceiver(BroadcastReceiver receiver, IntentFilter filter, String broadcastPermission, Handler scheduler)
å ¸åçåæ³ç¤ºä¾å¦ä¸ï¼
1 public class MainActivity extends Activity {
2 public static final String BROADCAST_ACTION = "com.example.corn";
3 private BroadcastReceiver mBroadcastReceiver;
4
5 @Override
6 protected void onCreate(Bundle savedInstanceState) {
7 super.onCreate(savedInstanceState);
8 setContentView(R.layout.activity_main);
9
mBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BROADCAST_ACTION);
registerReceiver(mBroadcastReceiver, intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mBroadcastReceiver);
}
}
注ï¼Androidä¸ææä¸è§å¯è 模å¼æå ³ç设计ä¸ï¼ä¸æ¦æ¶åå°registerï¼å¿ å®å¨ç¸åºçæ¶æºéè¦unregisterãå æ¤ï¼ä¸ä¾å¨onDestroy()åå°ä¸éè¦unregisterReceiver(mBroadcastReceiver)ã
å½æ¤Activityå®ä¾åæ¶ï¼ä¼å¨æå°MyBroadcastReceiver注åå°ç³»ç»ä¸ãå½æ¤Activityéæ¯æ¶ï¼å¨æ注åçMyBroadcastReceiverå°ä¸åæ¥æ¶å°ç¸åºç广æã
3.广æåéå广æç±»å
ç»å¸¸è¯´âåé广æâåâæ¥æ¶âï¼è¡¨é¢ä¸ç广æä½ä¸ºAndroid广ææºå¶ä¸çå®ä½ï¼å®é ä¸è¿ä¸å®ä½æ¬èº«æ¯å¹¶ä¸æ¯ä»¥æè°çâ广æâ对象åå¨çï¼èæ¯ä»¥âæå¾âï¼Intentï¼å»è¡¨ç¤ºãå®ä¹å¹¿æçå®ä¹è¿ç¨ï¼å®é å°±æ¯ç¸åºå¹¿æâæå¾âçå®ä¹è¿ç¨ï¼ç¶åéè¿å¹¿æåéè å°æ¤âæå¾âåéåºå»ã被ç¸åºçBroadcastReceiveræ¥æ¶åå°ä¼åè°onReceive()å½æ°ã
ä¸æ®µä»£ç ç段æ¾ç¤ºçæ¯ä¸ä¸ªæ®é广æçå®ä¹è¿ç¨ï¼å¹¶åéåºå»ãå ¶ä¸setAction(..)对åºäºBroadcastReceiverä¸çintentFilterä¸çactionã
1 Intent intent = new Intent();
2 intent.setAction(BROADCAST_ACTION);
3 intent.putExtra("name", "qqyumidi");
4 sendBroadcast(intent);
æ ¹æ®å¹¿æçåéæ¹å¼ï¼å¯ä»¥å°å ¶å为以ä¸å ç§ç±»åï¼
1.Normal Broadcastï¼æ®é广æ
2.System Broadcast: ç³»ç»å¹¿æ
3.Ordered broadcastï¼æåºå¹¿æ
4.Sticky Broadcastï¼ç²æ§å¹¿æ(å¨ android 5.0/api ä¸deprecated,ä¸åæ¨è使ç¨ï¼ç¸åºçè¿æç²æ§æåºå¹¿æï¼åæ ·å·²ç»deprecated)
5.Local Broadcastï¼Appåºç¨å 广æ
ä¸é¢åå«æ»ç»ä¸åç§ç±»åçåéæ¹å¼åå ¶ç¹ç¹ã
1).Normal Broadcastï¼æ®é广æ
æ¤å¤å°æ®é广æçå®ä¸ºï¼å¼åè èªå·±å®ä¹çintentï¼ä»¥context.sendBroadcast_"AsUser"(intent, ...)å½¢å¼ãå ·ä½å¯ä»¥ä½¿ç¨çæ¹æ³æï¼
sendBroadcast(intent)/sendBroadcast(intent, receiverPermission)/sendBroadcastAsUser(intent, userHandler)/sendBroadcastAsUser(intent, userHandler,receiverPermission)ã
æ®é广æä¼è¢«æ³¨åäºçç¸åºçæå ´è¶£ï¼intent-filterå¹é ï¼æ¥æ¶ï¼ä¸é¡ºåºæ¯æ åºçãå¦æåé广ææ¶æç¸åºçæéè¦æ±ï¼BroadCastReceiverå¦ææ³è¦æ¥æ¶æ¤å¹¿æï¼ä¹éè¦æç¸åºçæéã
2).System Broadcast: ç³»ç»å¹¿æ
Androidç³»ç»ä¸å ç½®äºå¤ä¸ªç³»ç»å¹¿æï¼åªè¦æ¶åå°ææºçåºæ¬æä½ï¼åºæ¬ä¸é½ä¼ååºç¸åºçç³»ç»å¹¿æãå¦ï¼å¼å¯å¯å¨ï¼ç½ç»ç¶ææ¹åï¼æç §ï¼å±å¹å ³éä¸å¼å¯ï¼ç¹äº®ä¸è¶³ççãæ¯ä¸ªç³»ç»å¹¿æé½å ·æç¹å®çintent-filterï¼å ¶ä¸ä¸»è¦å æ¬å ·ä½çactionï¼ç³»ç»å¹¿æååºåï¼å°è¢«ç¸åºçBroadcastReceiveræ¥æ¶ãç³»ç»å¹¿æå¨ç³»ç»å é¨å½ç¹å®äºä»¶åçæ¶ï¼æç³»ç»èªå¨ååºã
3)Ordered broadcastï¼æåºå¹¿æ
æåºå¹¿æçæåºå¹¿æä¸çâæåºâæ¯é对广ææ¥æ¶è èè¨çï¼æçæ¯åéåºå»ç广æ被BroadcastReceiveræç §å å循åºæ¥æ¶ãæåºå¹¿æçå®ä¹è¿ç¨ä¸æ®é广ææ å¼ï¼åªæ¯å ¶ç主è¦åéæ¹å¼å为ï¼sendOrderedBroadcast(intent, receiverPermission, ...)ã
对äºæåºå¹¿æï¼å ¶ä¸»è¦ç¹ç¹æ»ç»å¦ä¸ï¼
1>å¤ä¸ªå ·å½åå·²ç»æ³¨åä¸ææçBroadcastReceiveræ¥æ¶æåºå¹¿ææ¶ï¼æ¯æç §å å顺åºæ¥æ¶çï¼å å顺åºå¤å®æ åéµå¾ªä¸ºï¼å°å½åç³»ç»ä¸ææææçå¨æ注ååéæ注åçBroadcastReceiveræç §priorityå±æ§å¼ä»å¤§å°å°æåºï¼å¯¹äºå ·æç¸åçpriorityçå¨æ广æåéæ广æï¼å¨æ广æä¼æå¨åé¢ã
2>å æ¥æ¶çBroadcastReceiverå¯ä»¥å¯¹æ¤æåºå¹¿æè¿è¡æªæï¼ä½¿åé¢çBroadcastReceiverä¸åæ¥æ¶å°æ¤å¹¿æï¼ä¹å¯ä»¥å¯¹å¹¿æè¿è¡ä¿®æ¹ï¼ä½¿åé¢çBroadcastReceiveræ¥æ¶å°å¹¿æå解æå¾å°é误çåæ°å¼ãå½ç¶ï¼ä¸è¬æ åµä¸ï¼ä¸å»ºè®®å¯¹æåºå¹¿æè¿è¡æ¤ç±»æä½ï¼å°¤å ¶æ¯é对系ç»ä¸çæåºå¹¿æã
4)Sticky Broadcastï¼ç²æ§å¹¿æ(å¨ android 5.0/api ä¸deprecated,ä¸åæ¨è使ç¨ï¼ç¸åºçè¿æç²æ§æåºå¹¿æï¼åæ ·å·²ç»deprecated)ã
æ¢ç¶å·²ç»deprecatedï¼æ¤å¤ä¸åå¤åæ»ç»ã
5)Local Broadcastï¼Appåºç¨å 广æï¼æ¤å¤çAppåºç¨ä»¥Appåºç¨è¿ç¨ä¸ºçï¼
ç±åæéè¿°å¯ç¥ï¼Androidä¸ç广æå¯ä»¥è·¨è¿ç¨çè³è·¨Appç´æ¥éä¿¡ï¼ä¸æ³¨åæ¯exported对äºæintent-filterçæ åµä¸é»è®¤å¼æ¯trueï¼ç±æ¤å°å¯è½åºç°å®å ¨éæ£å¦ä¸ï¼
1.å ¶ä»Appå¯è½ä¼é对æ§çååºä¸å½åApp intent-filterç¸å¹é ç广æï¼ç±æ¤å¯¼è´å½åAppä¸ææ¥æ¶å°å¹¿æ并å¤çï¼
2.å ¶ä»Appå¯ä»¥æ³¨åä¸å½åAppä¸è´çintent-filterç¨äºæ¥æ¶å¹¿æï¼è·å广æå ·ä½ä¿¡æ¯ã
æ 论åªç§æ å½¢ï¼è¿äºå®å ¨éæ£é½ç¡®å®æ¯åå¨çãç±æ¤ï¼æ常è§çå¢å å®å ¨æ§çæ¹æ¡æ¯ï¼
1.对äºåä¸Appå é¨åéåæ¥æ¶å¹¿æï¼å°exportedå±æ§äººä¸ºè®¾ç½®æfalseï¼ä½¿å¾éæ¬Appå é¨ååºçæ¤å¹¿æä¸è¢«æ¥æ¶ï¼
2.å¨å¹¿æåéåæ¥æ¶æ¶ï¼é½å¢å ä¸ç¸åºçpermissionï¼ç¨äºæééªè¯ï¼
3.åé广ææ¶ï¼æå®ç¹å®å¹¿ææ¥æ¶å¨æå¨çå åï¼å ·ä½æ¯éè¿intent.setPackage(packageName)æå®å¨ï¼è¿æ ·æ¤å¹¿æå°åªä¼åéå°æ¤å ä¸çAppå ä¸ä¹ç¸å¹é çææ广ææ¥æ¶å¨ä¸ã
Appåºç¨å 广æå¯ä»¥ç解æä¸ç§å±é¨å¹¿æçå½¢å¼ï¼å¹¿æçåéè åæ¥æ¶è é½åå±äºä¸ä¸ªAppãå®é çä¸å¡éæ±ä¸ï¼Appåºç¨å 广æç¡®å®å¯è½éè¦ç¨å°ãåæ¶ï¼ä¹æ以使ç¨åºç¨å 广ææ¶ï¼èä¸æ¯ä½¿ç¨å ¨å±å¹¿æçå½¢å¼ï¼æ´å¤çèèå°çæ¯Android广ææºå¶ä¸çå®å ¨æ§é®é¢ã
ç¸æ¯äºå ¨å±å¹¿æï¼Appåºç¨å 广æä¼å¿ä½ç°å¨ï¼
1.å®å ¨æ§æ´é«ï¼
2.æ´å é«æã
为æ¤ï¼Android v4å ¼å®¹å ä¸ç»åºäºå°è£ 好çLocalBroadcastManagerç±»ï¼ç¨äºç»ä¸å¤çAppåºç¨å ç广æé®é¢ï¼ä½¿ç¨æ¹å¼ä¸ä¸é常çå ¨å±å¹¿æå ä¹ç¸åï¼åªæ¯æ³¨å/åæ¶æ³¨å广ææ¥æ¶å¨ååé广ææ¶å°ä¸»è°contextåæäºLocalBroadcastManagerçåä¸å®ä¾ã
代ç ç段å¦ä¸ï¼
1 //registerReceiver(mBroadcastReceiver, intentFilter);
2 //注ååºç¨å 广ææ¥æ¶å¨
3 localBroadcastManager = LocalBroadcastManager.getInstance(this);
4 localBroadcastManager.registerReceiver(mBroadcastReceiver, intentFilter);
5
6 //unregisterReceiver(mBroadcastReceiver);
7 //åæ¶æ³¨ååºç¨å 广ææ¥æ¶å¨
8 localBroadcastManager.unregisterReceiver(mBroadcastReceiver);
9
Intent intent = new Intent();
intent.setAction(BROADCAST_ACTION);
intent.putExtra("name", "qqyumidi");
//sendBroadcast(intent);
//åéåºç¨å 广æ
localBroadcastManager.sendBroadcast(intent);
4.ä¸å注åæ¹å¼ç广ææ¥æ¶å¨åè°onReceive(context, intent)ä¸çcontextå ·ä½ç±»å
1).对äºéæ注åçContextReceiverï¼åè°onReceive(context, intent)ä¸çcontextå ·ä½æçæ¯ReceiverRestrictedContextï¼
2).对äºå ¨å±å¹¿æçå¨æ注åçContextReceiverï¼åè°onReceive(context, intent)ä¸çcontextå ·ä½æçæ¯Activity Contextï¼
3).对äºéè¿LocalBroadcastManagerå¨æ注åçContextReceiverï¼åè°onReceive(context, intent)ä¸çcontextå ·ä½æçæ¯Application Contextã
注ï¼å¯¹äºLocalBroadcastManageræ¹å¼åéçåºç¨å 广æï¼åªè½éè¿LocalBroadcastManagerå¨æ注åçContextReceiverææå¯è½æ¥æ¶å°ï¼éæ注åæå ¶ä»æ¹å¼å¨æ注åçContextReceiveræ¯æ¥æ¶ä¸å°çï¼ã
5.ä¸åAndroid APIçæ¬ä¸å¹¿ææºå¶ç¸å ³APIéè¦åè¿
1).Android5.0/API level å¼å§ç²æ»å¹¿æåæåºç²æ»å¹¿æè¿æï¼ä»¥åä¸å建议使ç¨ï¼
2).âéæ注åç广ææ¥æ¶å¨å³ä½¿appå·²ç»éåºï¼ä¸»è¦æç¸åºç广æååºï¼ä¾ç¶å¯ä»¥æ¥æ¶å°ï¼ä½æ¤ç§æè¿°èªAndroid 3.1å¼å§æå¯è½ä¸åæç«â
Android 3.1å¼å§ç³»ç»å¨Intentä¸å¹¿æç¸å ³çflagå¢å äºåæ°ï¼åå«æ¯FLAG_INCLUDE_STOPPED_PACKAGESåFLAG_EXCLUDE_STOPPED_PACKAGESã
FLAG_INCLUDE_STOPPED_PACKAGESï¼å å«å·²ç»åæ¢çå ï¼åæ¢ï¼å³å æå¨çè¿ç¨å·²ç»éåºï¼
FLAG_EXCLUDE_STOPPED_PACKAGESï¼ä¸å å«å·²ç»åæ¢çå
主è¦åå å¦ä¸ï¼
èªAndroid3.1å¼å§ï¼ç³»ç»æ¬èº«åå¢å äºå¯¹ææappå½åæ¯å¦å¤äºè¿è¡ç¶æçè·è¸ªãå¨åé广ææ¶ï¼ä¸ç®¡æ¯ä»ä¹å¹¿æç±»åï¼ç³»ç»é»è®¤ç´æ¥å¢å äºå¼ä¸ºFLAG_EXCLUDE_STOPPED_PACKAGESçflagï¼å¯¼è´å³ä½¿æ¯éæ注åç广ææ¥æ¶å¨ï¼å¯¹äºå ¶æå¨è¿ç¨å·²ç»éåºçappï¼åæ ·æ æ³æ¥æ¶å°å¹¿æã
详æ åå Androidå®æ¹ææ¡£ï¼androiddocs/src/trunk/core/java/android/content/BroadcastReceiver.java
public void sendFinished(IActivityManager am) {
synchronized (this) {
if (mFinished) {
throw new IllegalStateException("Broadcast already finished");
}
mFinished = true;
估计是重复调用finish方法去结束广播,第二次调用就会抛这个错,flux 源码你不能关闭两次。trafficstats 源码
老祖源码2024-12-25 13:50
2024-12-25 12:57
2024-12-25 12:55
2024-12-25 12:50
2024-12-25 12:38
2024-12-25 12:18
2024-12-25 11:59
2024-12-25 11:41