1.Nacos 注册服务源码分析
2.Android基础知识——四大组件
3.Android N å大ç»ä»¶çå·¥ä½åç
4.android broadreceiver è¿è¡å¨åªä¸ªè¿ç¨
Nacos 注册服务源码分析
Nacos 注册服务源码分析
首先,从nacos-example样例工程入手,注册注册寻找注册服务的源码源码YY源码网关键入口。在NamingExample的静态静态main方法中,我们关注的注册注册招聘 开发 nginx 源码两行代码揭示了整个过程的起点。
从NamingFactory#createNamingService开始,源码源码unreal 4.4 源码 下载这个方法通过构造函数创建了一个NacosNamingService。静态静态值得注意的注册注册是,虽然创建过程看似简单,源码源码但构造方法中包含了属性的静态静态初始化和处理,这在非Spring项目中尤为重要,注册注册通常通过静态代码块或构造方法自行完成。源码源码
真正注册服务的静态静态lol网吧代理源码核心在于registerInstance方法。这个方法内部调用了clientProxy.registerService,注册注册跟踪这个过程是源码源码小猪pigcms原版源码理解Nacos注册服务的关键。
进一步追踪NamingService的构造方法,可以看到它内部创建了NamingClientProxyDelegate代理类。这个代理类实际上是设计模式中的代理模式,用于将请求委托给grpcClientProxy或.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å®æ¹ææ¡£ï¼/about/versions/android-3.1.html#launchcontrols
ç±æ¤ï¼å¯¹äºç³»ç»å¹¿æï¼ç±äºæ¯ç³»ç»å é¨ç´æ¥ååºï¼æ æ³æ´æ¹æ¤intent flagå¼ï¼å æ¤ï¼3.1å¼å§å¯¹äºéæ注åçæ¥æ¶ç³»ç»å¹¿æçBroadcastReceiverï¼å¦æAppè¿ç¨å·²ç»éåºï¼å°ä¸è½æ¥æ¶å°å¹¿æã
ä½æ¯å¯¹äºèªå®ä¹ç广æï¼å¯ä»¥éè¿å¤åæ¤flag为FLAG_INCLUDE_STOPPED_PACKAGESï¼ä½¿å¾éæ注åçBroadcastReceiverï¼å³ä½¿æå¨Appè¿ç¨å·²ç»éåºï¼ä¹è½è½æ¥æ¶å°å¹¿æï¼å¹¶ä¼å¯å¨åºç¨è¿ç¨ï¼ä½æ¤æ¶çBroadcastReceiveræ¯éæ°æ°å»ºçã
1 Intent intent = new Intent();
2 intent.setAction(BROADCAST_ACTION);
3 intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
4 intent.putExtra("name", "qqyumidi");
5 sendBroadcast(intent);
注1ï¼å¯¹äºå¨æ注åç±»åçBroadcastReceiverï¼ç±äºæ¤æ³¨åååæ¶æ³¨åå®å¨å ¶ä»ç»ä»¶ï¼å¦Activityï¼ä¸è¿è¡ï¼å æ¤ï¼ä¸åæ¤æ¹åå½±åã
注2ï¼å¨3.1以åï¼ç¸ä¿¡ä¸å°appå¯è½éè¿éæ注åæ¹å¼çå¬åç§ç³»ç»å¹¿æï¼ä»¥æ¤è¿è¡ä¸äºä¸å¡ä¸çå¤çï¼å¦å³æ¶appå·²ç»éåºï¼ä»ç¶è½æ¥æ¶å°ï¼å¯ä»¥å¯å¨serviceç..ï¼,3.1åï¼éæ注åæ¥å广ææ¹å¼çæ¹åï¼å°ç´æ¥å¯¼è´æ¤ç±»æ¹æ¡ä¸åå¯è¡ãäºæ¯ï¼éè¿å°Serviceä¸Appæ¬èº«è®¾ç½®æä¸åçè¿ç¨å·²ç»æ为å®ç°æ¤ç±»éæ±çå¯è¡æ¿ä»£æ¹æ¡ã