Android 为应用创建多个桌面快捷方式,可在卸载时自动删除桌面快捷方式
来源:互联网 发布:看图软件acdsee9.0 编辑:程序博客网 时间:2024/05/21 17:15
引言:有时候,我们需要多个app入口的快捷方式,其实很简单。闲话少说,看下面:
为应用创建快捷方式目前有两种方法:
一、权限:
1. 在AndroidManifest.xml中添加权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
对于第一个个程序入口MainActivity,在AndroidManifest.xml里设置:
<activity android:name=".MainActivity" android:icon="@drawable/icon" android:label="@string/app_name" android:process=":process.main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
对于第二个个程序入口SecondActivity,在AndroidManifest.xml里设置:
注:@style/translucent是在该页面设置的自定义透明窗体,
SecondActivity作为另一个入口,让用户进入app的另一个页面,访问指定功能。
<activity android:name=".SecondActivity" android:icon="@drawable/icon1" android:label="@string/app_name1" android:launchMode="singleInstance" android:theme="@style/translucent" > <intent-filter> <action android:name="android.intent.action.MAIN" /> </intent-filter> </activity>
二、添加方法:
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;}
2.hasInstallShortcut(Context context)如果返回false,表明没有快捷方式,那就执行创建快捷方式操作方法:
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");// 创建快捷方式的IntentIntent 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);// 发送广播。OKsendBroadcast(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();}
这样做完以后,就搞定了。卸载时,快捷方式自动删除。
三、自定义删除快捷方式方法:- /**
- * 删除快捷方式
- * */
- public static void deleteShortCut(Context context)
- {
- Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
- //快捷方式的名称
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,context.getString(R.string.app_name));
- /**删除和创建需要对应才能找到快捷方式并成功删除**/
- Intent intent = new Intent();
- intent.setClass(context, context.getClass());
- intent.setAction("android.intent.action.MAIN");
- intent.addCategory("android.intent.category.LAUNCHER");
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);
- context.sendBroadcast(shortcut);
- }
- Android 为应用创建多个桌面快捷方式,可在卸载时自动删除桌面快捷方式
- Android 为应用创建、删除桌面快捷方式
- Android为应用在桌面创建快捷方式
- Android 应用桌面快捷方式 创建 删除
- 【ANDROID】创建/删除桌面快捷方式
- android 应用创建桌面快捷方式
- Android在桌面创建快捷方式
- 桌面创建删除快捷方式
- [Android] Android应用启动后自动创建桌面快捷方式
- Android笔记:Android应用启动后自动创建桌面快捷方式
- android 创建桌面快捷方式
- Android 创建桌面快捷方式
- android创建桌面快捷方式
- Android 创建桌面快捷方式
- android 创建桌面快捷方式
- Android 创建桌面快捷方式
- Android创建桌面快捷方式
- android 创建桌面快捷方式
- python可视化--matplotlib
- writing idiomatic python 读书笔记(8)
- 运行React-Native例子UIExplorer
- 最常用的Java设计之一 观察者模式 在android中的实例 ListView Adapter机制
- C:\WINDOWS\Installer文件夹的安全清理
- Android 为应用创建多个桌面快捷方式,可在卸载时自动删除桌面快捷方式
- React-Native中的布局
- iOS 给app添加启动图
- HTML5开头声明
- Java基础--反射
- 数据库系统概论复习总结2 --- 第二章关系数据库
- linux查看历史命令history
- C语言简易的硬盘操作器
- dorado框架中的级联实现