Android创建快捷方式,Android检查快捷方式,Android删除快捷方式
16lz
2021-12-04
Android创建快捷方式,Android检查快捷方式,Android删除快捷方式
我直接提供一个类出来吧,可以直接copy使用
/** * @author YOLANDA * @Time 2015年3月14日 下午4:04:35 */public class ShortCutUtils {private Context context;public ShortCutUtils(Context context) {this.context = context;}/** * 创建快捷方式 * @author YOLANDA */public void createShortCut() {// <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher));intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name));// 不允许重复创建 一些实体机上无效,如三星 galaxy noteintent.putExtra("duplicate", false);Intent sIntent = new Intent(Intent.ACTION_MAIN);sIntent.addCategory(Intent.CATEGORY_LAUNCHER);// 加入action,和category之后,程序卸载的时候才会主动将该快捷方式也卸载sIntent.setClass(context, WelActivity.class);intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, sIntent);context.sendBroadcast(intent);}/** * 检查快捷方式 * @author YOLANDA * @return */public boolean hasShortCut() {// 测试发现,模拟器和moto平板M604可以正常判断。而三星I9000,HTC G6,三星GALAXY NOTE无法判断/*<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>*/String url = "";if (Build.VERSION.SDK_INT < 8) {url = "content://com.android.launcher.settings/favorites?notify=true";} else {url = "content://com.android.launcher2.settings/favorites?notify=true";}ContentResolver resolver = context.getContentResolver();Cursor cursor = resolver.query(Uri.parse(url), null, "title=?", new String[] { context.getString(R.string.app_name) }, null);if (cursor != null && cursor.moveToFirst()) {cursor.close();return true;}return false;}/** * 卸载快捷方式 * @author YOLANDA */public void delShortcut() {/*<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />*/Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");// 快捷方式的名称shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name));// 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer// 注意: ComponentName的第二个参数必须是完整的类名(包名+类名),否则无法删除快捷方式Intent sIntent = new Intent(Intent.ACTION_MAIN);sIntent.addCategory(Intent.CATEGORY_LAUNCHER);// 加入action,和category之后,程序卸载的时候才会主动将该快捷方式也卸载sIntent.setClass(context, MainActivity.class);shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, sIntent);context.sendBroadcast(shortcut);}}
更多相关文章
- Android百度地图开发(四):创建地图
- Android的设计模式-建造者模式
- Kubernetes 使用kubeadm创建集群
- Ubuntu20.04服务器+Anaconda上创建Python3.6虚拟环境并安装Pytor
- python中if和elif的区别介绍
- md5加密与数组函数
- 创建简历表格和注册表单
- 创建table表格和创建form表单并JS判断
- 创建html和常用标签