您的位置:首页 > 移动开发 > Android开发

android加ShortCut之历险记

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

代码如下:

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的能力。照样加个,问题解决。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: