博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Android Pro] 创建快捷方式,删除快捷方式,查询是否存在快捷方式
阅读量:7029 次
发布时间:2019-06-28

本文共 3393 字,大约阅读时间需要 11 分钟。

1: 创建快捷方式

需要权限: <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 

private static void createShortcut(Context cxt, String shortcutName, int shortcutIconRes,            String className, boolean duplicate, boolean laucherCategory) {        Intent intent = getShortCutIntent(cxt, cxt.getPackageName(), className, shortcutName,                laucherCategory);        int iconsize = cxt.getResources().getDimensionPixelSize(Res.dimen.app_icon_size);        BitmapDrawable icon = (BitmapDrawable) cxt.getResources().getDrawable(shortcutIconRes);        Bitmap bmp = ImageUtils.scaleTo(icon.getBitmap(), iconsize, iconsize, false);        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bmp);        intent.putExtra(EXTRA_SHORTCUT_DUPLICATE, duplicate);        // Now, notify the launcher to create the shortcut        cxt.sendBroadcast(intent);    }
private static Intent getShortCutIntent(Context cxt, String pkgName, String className,            String shortcutName, boolean laucherCategory) {        // Prepare the intents for shortcut        Intent shortcutIntent = new Intent(Intent.ACTION_VIEW);        shortcutIntent.setClassName(pkgName, className);        shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        shortcutIntent.putExtra(Constants.EXTRA_FROM_KEY, Constants.EXTRA_FROM_VALUE_SHORTCUT);        if (laucherCategory) {            shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);            shortcutIntent.setAction(Intent.ACTION_MAIN);        }        Intent intent = new Intent(ACTION_INSTALL_SHORTCUT);        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);        return intent;    }

2:删除快捷方式(MIUI系统不支持):

需要权限:<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

public static void removeShortcut(Context cxt, String shortcutName, String className,            boolean removeAll) {        // Prepare the intents for shortcut        Intent shortcutIntent = new Intent(Intent.ACTION_VIEW);        shortcutIntent.setClassName(cxt, className);        Intent intent = new Intent(ACTION_UNINSTALL_SHORTCUT);        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);        intent.putExtra(EXTRA_SHORTCUT_DUPLICATE, removeAll);        // Now, notify the launcher to remove the shortcut        cxt.sendBroadcast(intent);    }

3:查询快捷方式是否存在(三方rom大部分查询失败,cursor为null)

需要权限:<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />

  或者      <uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />

private boolean hasShortcut(){        boolean isInstallShortcut = false;        final ContentResolver cr = activity.getContentResolver();        final String AUTHORITY ="com.android.launcher.settings";        final Uri CONTENT_URI = Uri.parse("content://" +AUTHORITY + "/favorites?notify=true");        Cursor c = cr.query(CONTENT_URI,new String[] {"title","iconResource" },"title=?",        new String[] {getString(R.string.app_name).trim()}, null);        if(c!=null && c.getCount()>0){
//String title = c.getString(c.getColumnIndexOrThrow("title")); isInstallShortcut = true ; } return isInstallShortcut ; }

 

转载于:https://www.cnblogs.com/0616--ataozhijia/p/3940973.html

你可能感兴趣的文章
unity__DrawCall的理解
查看>>
springboot架构下运用shiro后在configuration,通过@Value获取不到值,总是为null
查看>>
SQLServer 数据库镜像+复制切换方案
查看>>
Postman初探
查看>>
仿淘宝头像上传功能(一)——前端篇。
查看>>
Eclipse通过集成svn实现版本控制
查看>>
OS开发过程中常用开源库
查看>>
关于在多个UItextield切换焦点
查看>>
hdu 2768
查看>>
git记住用户名密码
查看>>
ElasticSearch(2)-安装ElasticSearch
查看>>
从mysql数据表中随机取出一条记录
查看>>
ORACLE 锁表处理,解锁释放session
查看>>
深海机器人问题
查看>>
ios开发之 -- invalid nib registered for identifier
查看>>
正则表达式(括号)、[中括号]、{大括号}的区别小结
查看>>
88.NODE.JS加密模块CRYPTO常用方法介绍
查看>>
java.net.ProtocolException: Exceeded stated content-length of: '13824' bytes
查看>>
asp.net 连接 oracle10g 数据库
查看>>
C 入门 第十一节
查看>>