1.Android N å大ç»ä»¶çå·¥ä½åç
2.OpenHarmony 代码学习4:Ability子系统 源码解析(更新太快,跟不上步伐了)
3.å¦ä½è§£å³android 5.0ä¸åºç°çè¦åservice intent must be expl
4.Android UI线ç¨
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çæµç¨ç±»ä¼¼ã
OpenHarmony 代码学习4:Ability子系统 源码解析(更新太快,跟不上步伐了)
深入探讨OpenHarmony代码学习中关于Ability子系统的源码解析,重点关注基于monthly_的eclipse jsp源码代码架构与配置。
在源码解析中,SystemAbility的配置sa_profile至关重要,它确保了以c++实现的SA在加载注册逻辑时能够完成SA的注册,反之,未配置profile的System Ability将不会完成注册。可见abilitymgr等系统服务SA以特定方式运行,如.xml所示,uniapp底层源码ams的libabilityms.z.so在foundation进程中启动,并在启动后即向samgr组件注册SystemAbility,实现本地跨IPC访问。
进一步,分析AbilityManagerService作为SystemAbility的django源码程序管理器,提供管理Ability生命周期的管理能力。以AbilityManagerService::StartAbility为起点,此方法支持4种Startability,其中IRemoteObject属于分布式软总线子系统的ipc组件,负责进程间通信。双刃夺宝源码理解IPC与RPC机制,IPC与RPC在实现跨进程通信中扮演重要角色,IPC使用Binder驱动,适合设备内跨进程通信,而RPC采用软总线驱动,通化ios源码适用于跨设备跨进程通信。客户端与服务器通过客户端-服务器模型进行通信,通过代理获取服务提供方的接口进行数据交互。三方应用通过FA提供的接口绑定服务提供方的Ability,获取代理,实现通信。
在StartAbility中,callerToken由AbilityRuntime::AbilityContextImpl::StartAbility传入的AbilityContextImpl成员变量token_决定,通常指要启动的Ability。此调用链将在后续应用启动流程中总结,具体路径可参考官网介绍。
继续深入代码分析,观察StartAbility中的调用链,最终向BMS调用StartAbilityInner方法。根据ability类型的不同,启动方式也不同,已在代码段中进行了标注。在OpenHarmony代码学习中,PageAbility作为具备ArkUI实现的Ability,是最具直观性的用户可见并可交互的实例,通常由missionListManager启动。
å¦ä½è§£å³android 5.0ä¸åºç°çè¦åservice intent must be expl
æäºæ¶åæ们使ç¨Serviceçæ¶éè¦éç¨éç§å¯å¨çæ¹å¼ï¼ä½æ¯Android 5.0ä¸åºæ¥åï¼å ¶ä¸æ个ç¹æ§å°±æ¯Service Intent must be explitictï¼ä¹å°±æ¯è¯´ä»Lollipopå¼å§ï¼serviceæå¡å¿ é¡»éç¨æ¾ç¤ºæ¹å¼å¯å¨ã
èandroidæºç æ¯è¿æ ·åçï¼æºç ä½ç½®ï¼sdk/sources/android-/android/app/ContextImpl.javaï¼ï¼
private void validateServiceIntent(Intent service) {
if (service.getComponent() == null && service.getPackage() == null) {
if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
IllegalArgumentException ex = new IllegalArgumentException(
"Service Intent must be explicit: " + service);
throw ex;
} else {
Log.w(TAG, "Implicit intents with startService are not safe: " + service
+ " " + Debug.getCallers(2, 3));
}
}
}
å¤å¶ä»£ç
æ¢ç¶ï¼æºç éæ¯è¿æ ·åçï¼é£ä¹è¿éæ两ç§è§£å³æ¹æ³ï¼
1ã设置ActionåpackageNameï¼
åè代ç å¦ä¸ï¼
Intent mIntent = new Intent();
mIntent.setAction("XXX.XXX.XXX");//ä½ å®ä¹çserviceçaction
mIntent.setPackage(getPackageName());//è¿éä½ éè¦è®¾ç½®ä½ åºç¨çå å
context.startService(mIntent);
å¤å¶ä»£ç
æ¤æ¹å¼æ¯googleå®æ¹æ¨è使ç¨ç解å³æ¹æ³ã
2ãå°éå¼å¯å¨è½¬æ¢ä¸ºæ¾ç¤ºå¯å¨ï¼
public static Intent getExplicitIntent(Context context, Intent implicitIntent) {
// Retrieve all services that can match the given intent
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
// Make sure only one match was found
if (resolveInfo == null || resolveInfo.size() != 1) {
return null;
}
// Get component info and create ComponentName
ResolveInfo serviceInfo = resolveInfo.get(0);
String packageName = serviceInfo.serviceInfo.packageName;
String className = serviceInfo.serviceInfo.name;
ComponentName component = new ComponentName(packageName, className);
// Create a new intent. Use the old one for extras and such reuse
Intent explicitIntent = new Intent(implicitIntent);
// Set the component to be explicit
explicitIntent.setComponent(component);
return explicitIntent;
}
å¤å¶ä»£ç
è°ç¨æ¹å¼å¦ä¸ï¼
Intent mIntent = new Intent();
mIntent.setAction("XXX.XXX.XXX");
Intent eintent = new Intent(getExplicitIntent(mContext,mIntent));
context.startService(eintent);
Android UI线ç¨
æèï¼å å¿ é¡»äºè§£ä¸é¢2个é®é¢
1.顾åæä¹ UIçº¿ç¨ å°±æ¯å·æ°UI æå¨çº¿ç¨
2.UIæ¯å线ç¨å·æ°
1.对Activity æ¥è¯´ UI线ç¨å°±æ¯å ¶ä¸»çº¿ç¨
2.对Viewæ¥è¯´ UI线ç¨å°±æ¯å建ViewRootImplæå¨ç线ç¨
å¯ä»¥éè¿ WindowManager å é¨ä¼å建ViewRootImpl对象
好äºï¼è¿å ¥ä¸»é¢ãæ们æ¥æ ¢æ ¢æå¼é¢çº±ã
æ们å¯ä»¥åå«ä»å 个æ¹é¢åå ¥
æ们å¯è½é½æ使ç¨è¿ runOnUiThread ç°å¨æ¥çççæºç å®ç°ã
å¯ä»¥ä»ä¸é¢çæºç çå°
ä¸æ¯UIçº¿ç¨ å°±ç¨Handleråå°Handleræå¨ç线ç¨ä¸ï¼å¦ææ¯UI线ç¨ç´æ¥å°±è°ç¨runæ¹æ³ã
Activityçå建ï¼
1.Activityå建ï¼mInstrumentation.newActivity
2.å建Context ï¼ContextImpl appContextcreateBaseContextForActivity(r)
æ们ç»å¸¸ç¨è¿ä¸ªæ¹æ³å¹²çäºæ å°±æ¯ï¼è¦ä¹å¨onCreateä¸è·åView宽é«çå¼ãè¦ä¹å°±æ¯å¨å线ç¨ä¸åä¸äºèæ¶æä½ ï¼ç¶åpoståå°å¯¹åºViewæå¨ççº¿ç¨ æ¥ç»å¶UIæä½ãé£ä¹è¿ä¸ªå¯¹åºç线ç¨å°±æ¯UI线ç¨äºã
é£ä¹è¿ä¸ªUI线ç¨å°±ä¸å®æ¯ä¸»çº¿ç¨åï¼
æ¥æ¥ç»§ç»æ¥çãå®çæºç View:post
mAttachInfo å¨dispatchAttachedToWindow ä¸è¢«èµå¼ ï¼ä¹å°±æ¯å¨ViewRootImplå建çæ¶åï¼æ以æ¯å建ViewRootImplæå¨ç线ç¨ã
attachInfo ä¸é¢æ¶å为null å¢ï¼å¨ViewRootImpl è¿æ²¡æ¥å¾åå建çæ¶åï¼ViewRootImpl å建æ¯å¨ âonResume" ä¹åãæä»¥å¨ Activity ç onCreate å»View.post é£ä¹AttachInfo æ¯ä¸ºnull ã
å½ AttachInfo == null é£ä¹ä¼è°ç¨ getRunQueue().post(action) ã
æç»è¿ä¸ªRunnable 被 ç¼åå° HandlerActionQueue ä¸ã
ç´å°ViewRootImpl ç performTraversals ä¸ è°ç¨dispatchAttachedToWindow(mAttachInfo, 0);ï¼ é£ä¹æä¼å»å¤ç RunQueue() ä¸çRunnableã
æ¥å¼ å¾ ä¾¿äºç解è¿ä¸ªæµç¨
æ们ææ¶åå»å线ç¨æä½UIçæ¶å(å¦ï¼requestLayout)ï¼ä¼å¾ç»å¸¸è§å°ä¸é¢ç æ¥éæ¥å¿ï¼
Only the original thread that created a view hierarchy can touch its views
为ä»ä¹ä¼æ¥è¿ä¸ªé误å¢ï¼
ç¿»è¯ä¸ä¸ï¼åªæå建è§å¾å±æ¬¡ç»æçåå§çº¿ç¨æè½æ¥è§¦å°å®çè§å¾ã
ä¹å°±æ¯æä½UIç线ç¨è¦åViewRootImplå建ç线ç¨æ¯åä¸ä¸ªçº¿ç¨æè¡ï¼å¹¶ä¸æ¯åªæ主线ç¨æè½æ´æ°UIåã
ViewRootImplå建ç线ç¨ï¼é£ä¹ ViewRootImpl å¨åªé被å建çå¢ï¼
ä»ä¸å¾å¯ä»¥çå°ViewRootImplå建æå¼å§æ¯ä» ActivityThread çHandleResumeActivityä¸å¼å§ ä¸ç´ ViewRootImpl å建ï¼ä¹å°±æ¯è¯´ViewRootImpl 对åºçUI线ç¨å ActivityThread å¨åä¸ä¸ªçº¿ç¨ ä¹å°±æ¯ä¸»çº¿ç¨ã
å¥½äº éè¿ä¸é¢ç讲解ï¼ä¸é¢çé®é¢ç¸ä¿¡ä½ å¯ä»¥èªå·±åçå¦~