我希望我的应用程序有时充当家庭启动器,有时不充当。为了做到这一点,我实现了一个“别名活动”,它将作为“主页启动器活动”(见下面的清单)。
在启用和禁用之间切换后,有时我在应用程序列表中看不到应用程序(我在意图过滤器中声明了启动器)。但是,重新启动设备后,我可以在列表中看到应用程序......非常奇怪......
我通过以下代码在两个活动之间切换:
启用活动:
ComponentName enableHomeActivityManifest = new ComponentName(HOME_PACKAGE,HOME_PACKAGE
+ ".activities.HomeActivity");
mContext.getPackageManager().setComponentEnabledSetting(enableHomeActivityManifest, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
禁用别名:
ComponentName componentToDisable = new ComponentName(this, MessageDispatcher.HOME_PACKAGE + ".aliasHomeActivity");
getPackageManager().setComponentEnabledSetting(componentToDisable,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
);
显现:
<activity
android:name=".activities.HomeActivity"
android:clearTaskOnLaunch="true"
android:configChanges="orientation"
android:icon="@drawable/home"
android:label="@string/app_name"
android:launchMode="singleTask"
android:exported="true"
android:screenOrientation="portrait"
android:stateNotNeeded="true"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY" />
</intent-filter>
</activity>
<activity-alias
android:name=".aliasHomeActivity"
android:clearTaskOnLaunch="true"
android:configChanges="orientation"
android:enabled="false"
android:icon="@drawable/home"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:stateNotNeeded="true"
android:exported="true"
android:targetActivity=".activities.HomeActivity"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<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" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>