2011.09.09(2)——— android 桌面添加快捷方式
参考:
http://www.apkbus.com/android-5728-1-1.html
添加快捷方式:
private void installShortCut(){
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
// 是否可以有多个快捷方式的副本,参数如果是true就可以生成多个快捷方式,如果是false就不会重复添加
shortcutIntent.putExtra("duplicate", false);
Intent mainIntent = new Intent(Intent.ACTION_MAIN);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// 要删除的应用程序的ComponentName,即应用程序包名+activity的名字
//intent2.setComponent(new ComponentName(this.getPackageName(), this.getPackageName() + ".MainActivity"));
mainIntent.setClass(this, this.getClass());
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mainIntent);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
sendBroadcast(shortcutIntent);
} sendBroadcast(shortcutIntent);
权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
总不能每次进入应用 都添加一次快捷方式 所以 需要判断一下快捷方式是否已经存在
private boolean hasShortCut() {
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(Uri.parse("content://com.android.launcher.settings/favorites?notify=true"), null, "title=?",
new String[] {getString(R.string.app_name)}, null);
if (cursor != null && cursor.moveToFirst()) {
cursor.close();
return true;
}
return false;
}
查询权限:
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
删除快捷方式:
private void uninstallShortcut(){
System.out.println("11");
Intent intent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setClass(this, this.getClass()));
sendBroadcast(intent);
}
权限:
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>