android加ShortCut之历险记

本文介绍了一种解决Android应用中快捷方式无法正常启动的问题的方法,包括通过修改应用配置文件来降低目标版本号,以及在AndroidManifest.xml中正确配置intent-filter,从而允许在桌面生成美观的图标并实现快捷启动。

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

需要在主入口外再加一个快捷方式,平常的加快捷方式代码很常见,一搜一大把,现在贡献下。让同学们少一点奔劳。

代码如下:

 private void addShortcut() {

        // 安装的Intent
        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

        // 快捷名称
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name_alias));
        shortcut.putExtra("duplicate", false);
        Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
        shortcutIntent.setClassName(this.getPackageName(), this.getPackageName()+"."+"SafeBoxActivity");
        shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        // 快捷图标
        Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon2);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
        // 发送广播
        sendBroadcast(shortcut);

    }
在AndroidMainifest.xml中添加:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"></uses-permission>
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"></uses-permission>
    

然后,调用  addShortcut,在桌面上生成了好看的图标。高兴的点一点,完蛋,提示“程序未安装”,什么意思,命名安装了吧,只是另一个

快捷方式而已。很无奈。

还好我们是unix下的java程序猿,我们知道看log.打开idea的log栏目,然后点击,我们搜素到这么一个日志。

Make sure to create a MAIN intent-filter for the corresponding activity or use the exported attribute for this activity. tag=ShortcutInfo

 Launcher does not have the permission to launch Intent 

解决:

随之google下,记得是google,不是百度啊。

在stackoverflow里找到一个这么答案。

讲targetVersion降级到19,就ok。

<uses-sdk android:minSdkVersion="8" tools:ignore="UsesMinSdkAttributes" android:targetSdkVersion="19"/>

试试。果然搞定。

另一种思路,记得58同城也这么干过,在主启动图标外,另外搞了一个叫生活工具箱的启动图标。对58程序进行分析,在另起的shortcut工具箱配置里发现这句。

<activity android:configChanges="locale|keyboardHidden" android:name="com.wuba.plugins.ThirdFolderActivity" android:screenOrientation="portrait" android:taskAffinity="com.wuba.affinity_third_folder" android:theme="@style/DialogActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <action android:name="android.intent.action.MAIN"/>
            </intent-filter>
        </activity>
原来是这样啊,如果设置了这个filter,可以在原有shortcut外,再赋予一个启动另一个包里activity的能力。照样加个,问题解决。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值