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

Android 为某个App 添加/移除 启动快捷方式、书签快捷方式、应用市场快捷方式

2013-07-15 20:40 465 查看
1. 在AndroidManifest文件中添加uses permission

<!-- 添加快捷方式 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<!-- 移除快捷方式 -->
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
2. 为其他应用添加应用程序的快捷方式(以微信为例)
String packageName = "com.tencent.mm";//以微信为例
PackageManager pManager = context.getPackageManager();
Intent launchIntent = pManager.getLaunchIntentForPackage(packageName);

List<ResolveInfo> infoList = pManager.queryIntentActivities(launchIntent, PackageManager.GET_ACTIVITIES);

ResolveInfo launchInfo = infoList.get(0);
String name = launchInfo.loadLabel(pManager).toString();
String launchActName = launchInfo.activityInfo.name;
int iconId = launchInfo.activityInfo.applicationInfo.icon;

Intent addShortcutIntent = new Intent(ACTION_ADD_SHORTCUT);
// 添加名称
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
// 添加图标
Context pkgContext;
try {
pkgContext = context.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(pkgContext, iconId);
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

// 添加Component
ComponentName compName = new ComponentName(packageName, launchActName);
Intent extraIntent = new Intent(Intent.ACTION_MAIN);
extraIntent.setComponent(compName);
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, extraIntent);

// 不允许重复添加
addShortcutIntent.putExtra("duplicate", false);
context.sendBroadcast(addShortcutIntent);

3. 添加书签快捷方式/应用市场快捷方式(以微信为例)

// 以微信为例
Parcelable icon = Intent.ShortcutIconResource.fromContext(context, R.drawable.wechat_logo);
String name = "wechat";
String package = "com.tencent.mm";

Intent intentAddShortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 添加名称
intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
// 添加图标
intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

Intent launchIntent = new Intent(Intent.ACTION_VIEW);
// 添加应用市场链接
String appAddr = "market://details?id=" + packageName;
// 添加网页链接
// String appAddr = "http://www.baidu.com";
launchIntent.setData(Uri.parse(appAddr));

intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intentAddShortcut.putExtra("duplicate", false);

context.sendBroadcast(intentAddShortcut

4. 移除快捷方式:

移除快捷方式与添加快捷方式几乎完全相同,唯一的不同是移除的Intent的Action是"com.android.launcher.action.UNINSTALL_SHORTCUT";

5. 判断手机上是否安装了某个App

public boolean hasApp(String packageName) {
PackageManager pManager = context.getPackageManager();
try {
pManager.getApplicationInfo(packageName, 0);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}


备注:

问:如何获得某个应用的package name

答:进入豌豆荚市场,搜索某个应用,进入其详情页面。在地址栏中可以查看到应用的包名:



附上我封装好的类:http://download.csdn.net/download/u011268102/5759383
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息