Android 创建桌面快捷方式研究
2013-06-07 21:55
375 查看
创建快捷方式本来是很简单的一件事件,代码网上一搜一大堆,但是你用后细心研究一下就会发现一个问题:
不是回到你返回桌面时正在运行的那个 Activity 而是重新运行了你应用的第一个 Activity ,
这样显然是不可以的,网上苦苦搜寻半天千篇一律都没解决办法,于是研究 Logcat
首先从 Launcher 点击图标系统发出的 Intent 如下:
I/ActivityManager(564): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.freesky.sdkproxytestbed/.MainActivity} from pid 29805
再看看从自己创建的快捷方式发出的 Intent:
I/ActivityManager(564): START u0 {act=android.intent.action.MAIN flg=0x10200000 cmp=com.freesky.sdkproxytestbed/.MainActivity bnds=[216,203][376,403]} from pid 29805
仔细对比下是不是少了:cat=[android.intent.category.LAUNCHER]
所以你只需要加上这个 Category 就OK了,奉上代码!
不是回到你返回桌面时正在运行的那个 Activity 而是重新运行了你应用的第一个 Activity ,
这样显然是不可以的,网上苦苦搜寻半天千篇一律都没解决办法,于是研究 Logcat
首先从 Launcher 点击图标系统发出的 Intent 如下:
I/ActivityManager(564): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.freesky.sdkproxytestbed/.MainActivity} from pid 29805
再看看从自己创建的快捷方式发出的 Intent:
I/ActivityManager(564): START u0 {act=android.intent.action.MAIN flg=0x10200000 cmp=com.freesky.sdkproxytestbed/.MainActivity bnds=[216,203][376,403]} from pid 29805
仔细对比下是不是少了:cat=[android.intent.category.LAUNCHER]
所以你只需要加上这个 Category 就OK了,奉上代码!
private String getAppName() { String appName = ""; try { PackageInfo info = this.getPackageManager().getPackageInfo( this.getPackageName(), 0); appName = getString(info.applicationInfo.labelRes); } catch (NameNotFoundException e) { } return appName; } private int getAppIcon() { int appIcon = 0; try { PackageInfo info = this.getPackageManager().getPackageInfo( this.getPackageName(), 0); appIcon = info.applicationInfo.icon; } catch (NameNotFoundException e) { } return appIcon; } public void createShortCut(Activity app, String componetName, String appName, int icon) { ComponentName comp = new ComponentName(this.getPackageName(), this.getPackageName() + "." + this.getLocalClassName()); Intent shortcutIntent = new Intent(Intent.ACTION_MAIN).setComponent(comp); //加上这句能避免启动新的 Activity shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER); Intent intent = new Intent(); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName); ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(app, icon); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); // 不创建重复快捷方式 intent.putExtra("duplicate", false); // 添加快捷方式 intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); app.sendBroadcast(intent); }
相关文章推荐
- Android创建桌面快捷方式几种方法
- [Android]为指定的应用创建桌面快捷方式
- Android创建桌面快捷方式两种方法
- Android创建桌面快捷方式并像启动Activity传递参数
- Android用程序在桌面创建app快捷方式
- Android 创建和删除程序桌面快捷方式(转)
- Android应用开发之(如何自动在桌面创建快捷方式)
- Android -- 创建桌面快捷方式
- [置顶] android,解决手动创建的桌面快捷方式无法跳转到制定的activity的问题,提示未安装应用程序
- Android程序首次运行自动创建桌面快捷方式的方法
- android 为应用程序创建桌面快捷方式技巧分享
- android直接创建桌面快捷方式
- android 创建桌面快捷方式
- Android 创建桌面快捷方式
- Android Launcher研究(四)-----------桌面应用快捷方式的开发!
- android 创建桌面快捷方式
- Android创建和删除桌面快捷方式 【转】
- Android Launcher研究(四)-----------桌面应用快捷方式的开发!
- Android程序创建桌面快捷方式
- Android 判断桌面是否快捷方式,不存在则创建