android中实现程序多入口的几种方法

本文介绍在Android应用中创建多个启动入口的方法,包括通过快捷方式、修改启动配置及使用别名活动等手段实现,并提供了具体实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(有两个图标同时存在)
一,通过快捷方式形式,
// 需添加权限<uses-permission android:name="com.android.launcher.action.INSTALL_SHORTCUT"/>
public static void createShrotcut(Activity activity, int iconResId, int nameResId){
Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
Intent.ShortcutIconResource icon = Intent.ShortcutIconResource.fromContext(activity, iconResId);//实现了parcelable接口
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,nameResId); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,newIntent(activity.getApplicationContext(),activity.getClass()));
intent.putExtra("duplicate",false);
activity.sendBroadcast(intent);
}
二,修改启动方式,(也可通过指定不同 :process.///)
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
三,通过别名 <activity-alias>

(只有一个图标)(动态更换图标))
代码如下:
public void updateIcon(View v){
PackageManager pm = getPackageManager();
ComponentName componentName = getComponentName();
Log.d("componentName()", componentName.getClassName());
pm.setComponentEnabledSetting(getComponentName(),PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(new ComponentName(getApplicationContext(),"com.example.myapplication.alias1"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
}
完整清单文件如下:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="原图标"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<activity-alias
android:name="alias2"
android:enabled="false"
android:icon="@mipmap/ic_launcher2"
android:label="新图标"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
实现多程序入口,会有BUG,请谨慎使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值