引言:有时候,我们需要多个app入口的快捷方式,其实很简单。闲话少说,看下面:
为应用创建快捷方式目前有两种方法:
一、权限:
1. 在AndroidManifest.xml中添加权限:
对于第一个个程序入口MainActivity,在AndroidManifest.xml里设置:
注:@style/translucent是在该页面设置的自定义透明窗体,
SecondActivity作为另一个入口,让用户进入app的另一个页面,访问指定功能。
二、添加方法:
1.获得快捷桌面的读取权限,如果有快捷方式,就返回true,否则false
public static boolean hasInstallShortcut(Context context) {
boolean hasInstall = false;
String AUTHORITY = "com.android.launcher.settings";
int systemversion = Build.VERSION.SDK_INT;
Log.i("Build.VERSION.SDK==========>", systemversion + "");
if (systemversion >= 8) {
AUTHORITY = "com.android.launcher2.settings";
}
Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/favorites?notify=true");
Cursor cursor = context
.getContentResolver()
.query(CONTENT_URI,
new String[] { "title" },
"title=?",
new String[] { context.getString(R.string.shortcutname) },
null);
if (cursor != null && cursor.getCount() > 0) {
hasInstall = true;
}
return hasInstall;
}
public void createShortCut(Context context) {
// ***************************快速打开APP的桌面快捷方式**********************************
Intent intent = new Intent();
intent.setClass(context, context.getClass());
/* 以下两句是为了在卸载应用的时候同时删除桌面快捷方式 */
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
// 创建快捷方式的Intent
Intent shortcutintent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 需要现实的名称
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.shortcutname));
// 不允许重复创建
shortcutintent.putExtra("duplicate", false);
// 快捷图片
Parcelable icon = Intent.ShortcutIconResource.fromContext(
getApplicationContext(), R.drawable.icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
// 点击快捷图片,运行的程序主入口
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
// 发送广播。OK
sendBroadcast(shortcutintent);
// ***************************快速操作的桌面快捷方式**********************************
Intent intent1 = new Intent();
intent1.setClass(context, SecondActivity.class);
/* 以下两句是为了在卸载应用的时候同时删除桌面快捷方式 */
intent1.setAction("android.intent.action.MAIN");
// intent1.addCategory("android.intent.category.LAUNCHER");
Intent shortcutintent1 = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent1.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name1));
shortcutintent1.putExtra("duplicate", false);
Parcelable icon1 = Intent.ShortcutIconResource.fromContext(
getApplicationContext(), R.drawable.icon1);
shortcutintent1.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon1);
shortcutintent1.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent1);
sendBroadcast(shortcutintent1);
}
3.为了防止重复创建,只在安装后第一次启动时创建,方法如下:
注:在自定义的Utils工具类里定义SHOW_CREATE_SHORTCUT_ICON常量,在Utils工具类类用该常量存储布尔值,以达到只创建一次快捷方式的目 的。
if (!Utils.getInstance(MainActivity.this).getBoolean(
Utils.SHOW_CREATE_SHORTCUT_ICON, false)
&& hasInstallShortcut(MainActivity.this) == false) {
// 创建桌面快捷方式
createShortCut(MainActivity.this);
Utils.getInstance(getApplicationContext()).save(
Utils.SHOW_CREATE_SHORTCUT_ICON, true);
// 提示用户桌面快捷方式创建成功
Toast.makeText(MainActivity.this, "快捷方式创建成功", Toast.LENGTH_SHORT)
.show();
}
这样做完以后,就搞定了。卸载时,快捷方式自动删除。
三、自定义删除快捷方式方法: