平台:
Amlogic s905X + Android 7.1
问题:
内置指定应用作为Launcher并默认启动(PS: 同时存在MboxLauncher).
开机后, 无法进入指定Launcher, 但是, 在输入HOME键后, 却可以显示存在两个Launcher,
通过代码检测, 也可以检测出指定Launcher存在.
分析:
|–frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
boolean startHomeActivityLocked(int userId, String reason) {
if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
&& mTopAction == null) {
// We are running in factory test mode, but unable to find
// the factory test app, so just sit around displaying the
// error message and don't try to start anything.
return false;
}
//新增代码:
Intent intent = null;
ActivityInfo aInfo = null;
final String spPkg = "com.android.launcher";
final String spCls = "com.android.launcher.Launcher";
if(!android.text.TextUtils.isEmpty(spPkg) && !android.text.TextUtils.isEmpty(spCls)){
/*源码中Launcher2的Intent包含:
* <action android:name="android.intent.action.MAIN" />
* <category android:name="android.intent.category.HOME" />
* <category android:name="android.intent.category.DEFAULT" />
* <category android:name="android.intent.category.MONKEY"/>
*/
intent = new Intent(mTopAction);
intent.addCategory(Intent.CATEGORY_HOME);
//指定包类名
intent.setComponent(new ComponentName(spPkg, spCls));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_DEBUG_TRIAGED_MISSING);
try{
List<ResolveInfo> ris = AppGlobals.getPackageManager().queryIntentActivities(intent, null, 0, userId).getList();
//异常时:ris.size == 0, 即找不到所想要的Activity组件
//正常时:ris.size == 1
if(ris != null && ris.size() > 0){
ResolveInfo rinfo = ris.get(0);
if(rinfo != null)aInfo = rinfo.activityInfo;
}
}catch(Exception e){e.printStackTrace();}
}
//以下为原有代码
if(aInfo == null){
intent = getHomeIntent();
aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
}
if (aInfo != null) {
intent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
// Don't do this if the home app is currently being
// instrumented.
aInfo = new ActivityInfo(aInfo);
aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
ProcessRecord app = getProcessRecordLocked(aInfo.processName,
aInfo.applicationInfo.uid, true);
if (app == null || app.instrumentationClass == null) {
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
mActivityStarter.startHomeActivityLocked(intent, aInfo, reason);
}
} else {
Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
}
return true;
}
逻辑上并没有问题, 问题的根本原因在:
|-- device/amlogic/XXX/overlay/vendor/amlogic/apps/MboxLauncher2/AndroidManifest.xml
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:directBootAware="true" >
<activity
android:name=".Launcher"
android:clearTaskOnLaunch="true"
android:label="@string/app_name"
android:launchMode="singleTask"
android:stateNotNeeded="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY" />
</intent-filter>
</activity>
</application>
指定Launcher 缺少了上面 android:directBootAware="true" 定义.
加入此属性后测试, 解决!