为应用创建快捷方式目前有两种方法:

1. 程序启动时主动添加快捷方式到桌面------------>主动添加

2.长按桌面,弹出应用选择窗,拖动应用到桌面---------->被动添加


公用方法:

/**  * 返回添加到桌面快捷方式的Intent:    * 1.给Intent指定action="com.android.launcher.INSTALL_SHORTCUT"  * 2.给定义为Intent.EXTRA_SHORTCUT_INENT的Intent设置与安装时一致的action(必须要有)    * 3.添加权限:com.android.launcher.permission.INSTALL_SHORTCUT  */ public static Intent getShortcutToDesktopIntent(Context context) { Intent intent = new Intent();  intent.setClass(context, context.getClass());          /*以下两句是为了在卸载应用的时候同时删除桌面快捷方式*/ intent.setAction("android.intent.action.MAIN");   intent.addCategory("android.intent.category.LAUNCHER");               Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");     // 不允许重建     shortcut.putExtra("duplicate", false);     // 设置名字     shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,context.getString(R.string.app_name));     // 设置图标     shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher));     // 设置意图和快捷方式关联程序     shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);     return shortcut; }


一、主动添加方式:

1. 在AndroidManifest.xml中添加权限:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

2. 在启动Activity中发送广播:

sendBroadcast(getShortcutToDesktopIntent(MainActivity.this));


二、被动添加方式:

1.在AndroidManifest.xml中添加权限:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

2.在AndroidManifest.xml中为主Activity添加action监听:

<!-- 如果是通过桌面长按添加快捷方式,才需要添加此配置 -->            <intent-filter>                <action android:name="android.intent.action.CREATE_SHORTCUT" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>  

3.在启动Activity中添加广播监听:

final Intent launchIntent = getIntent();final String action = launchIntent.getAction();if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {Log.i(TAG, "create shortcut method one---------------- ");setResult(RESULT_OK, ShortcutUtils.getShortcutToDesktopIntent(MainActivity.this));finish();} 

三、删除快捷方式:

1.在AndroidManifest.xml中添加权限:

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>

2.代码:

/**  * 删除快捷方式  * */ 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);           }


四、判断快捷方式是否已创建(该方法不起作用,方法中有说明和解决方案):

1.添加权限:

 <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>

2.代码:

/**  * 判断是否已添加快捷方式:    * 暂时没有方法能够准确的判断到快捷方式,原因是,1、不同厂商的机型他的快捷方式uri不同,我遇到过HTC的他的URI是content://com.htc.launcher.settings/favorites?notify=true2、桌面不只是android自带的,可能是第三方的桌面,他们的快捷方式uri都不同提供一个解决办法,创建快捷方式的时候保存到preference,或者建个文件在SD卡上,下次加载的时候判断不存在就先发删除广播,再重新创建  * 添加权限:<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" ></uses-permission>  */ 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 + "");     /*大于8的时候在com.android.launcher2.settings 里查询(未测试)*/     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.app_name) }, null);     if (cursor != null && cursor.getCount() > 0) {         hasInstall = true;     }     return hasInstall; }


demo下载地址:

点击打开链接



更多相关文章

  1. adb pull命令复制android数据库文件.db到电脑
  2. TableLayout 动态表格
  3. Android(安卓)文件系统的权限设置
  4. Android9.0 默认是禁止所有的http java.io.IOException: Clear
  5. (android)向sdcard中添加文件出现Failed to push the item(s)
  6. android studio 添加按钮事件的三种方法
  7. Android将Widget添加到自己的应用程序
  8. 已解决:Android向SD卡存储数据时出现java.io.FileNotFoundExcepti
  9. android permiss manager (安全软件获取权限 禁用 可用 提示状态)

随机推荐

  1. android、IOS 基于webview 与 HTML 的集
  2. android R 文件生成不了
  3. 从零搭建 repo 服务器
  4. Android线程优先级设置方法
  5. android笔记
  6. Android(安卓)4.4.2 exfat 移植
  7. Android界面编程——对话框控件(四)
  8. Drawable分类
  9. Activity生命周期详解
  10. 简述Android消息机制及解惑