一、判断是否已有快捷方式

    private String getAuthorityFromPermission(Context context, String permission){        if (permission == null) return null;        List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);        if (packs != null) {            for (PackageInfo pack : packs) {                 ProviderInfo[] providers = pack.providers;                 if (providers != null) {                     for (ProviderInfo provider : providers) {                         if (permission.equals(provider.readPermission)) return provider.authority;                        if (permission.equals(provider.writePermission)) return provider.authority;                    }                 }            }        }                return null;    }

private boolean hasShortcut(Context context,String shortCutName)    {        boolean has = false;        final ContentResolver cr = context.getContentResolver();        final String AUTHORITY = getAuthorityFromPermission(context, "com.android.launcher.permission.READ_SETTINGS");        final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");                //确认content Provider中是否有快捷键信息        Cursor c = cr.query(CONTENT_URI,                            new String[] {"title","iconResource" },                            "title=?",                            new String[] {shortCutString.trim()},                             null);        if(c != null && c.getCount() > 0){            has= true ;        }        return has;    }

二、添加快捷方式

private void addShortCut(Context mContext)    {                      boolean has = hasShortcut(mContext, mContext.getString(R.string.str_app_name));        if(has)        {            return;        }        Intent shortCutIntent = null;        int shortCutNameId = R.string.app_name;        int shortCutIconId = R.drawable.app_icon;        String pkg = PACKAGE_NAME;        boolean installed = isInstalledApp(mContext);        if(installed)        {            Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);            resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);            resolveIntent.setPackage(pkg);                      List<ResolveInfo> apps = mContext.getPackageManager().queryIntentActivities(resolveIntent, PackageManager.GET_ACTIVITIES);                        shortCutIntent = new Intent();            if((apps != null) && (apps.size() != 0))            {                shortCutIntent.setComponent(new ComponentName(pkg, apps.get(0).activityInfo.name));            }        }        else         {            shortCutIntent = new Intent(mContext.getApplicationContext(), AppActivity.class);        }                Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");                  //快捷方式的名称          shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mContext.getString(shortCutNameId));          shortcut.putExtra("duplicate", false); //不允许重复创建           //快捷方式的图标          ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(mContext.getApplicationContext(), shortCutIconId);          shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);                  //程序入口        shortCutIntent.setAction(Intent.ACTION_MAIN);        shortCutIntent.addCategory(Intent.CATEGORY_LAUNCHER);        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutIntent);        mContext.sendBroadcast(shortcut);       }

三、删除快捷方式

   private void deleteShortCut(Context mContext)    {        Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");        Intent shortCutIntent = new Intent(mContext.getApplicationContext(), AppActivity.class);        shortCutIntent.setAction(Intent.ACTION_MAIN);        shortCutIntent.addCategory(Intent.CATEGORY_LAUNCHER);        // 快捷方式的名称        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mContext.getString(R.string.app_name));        // 注意: ComponentName的第二个参数必须是完整的类名(包名+类名),否则无法删除快捷方式        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutIntent);        mContext.sendBroadcast(shortcut);    }

四、被快捷方式启动的Activity的Intent-filter

            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>    

更多相关文章

  1. eclipse 开发 android 快捷键!
  2. eclipse 开发 android 快捷键
  3. android studio常用的快捷键
  4. IDEA 快捷键 Android Studio快捷键
  5. 《Android 获取当前app的版本号和版本名称》
  6. android 获取连接WiFi的名称SSID
  7. Android Studio笔记(2)——快捷键
  8. Android studio快捷键大全+Android studio使用小技巧

随机推荐

  1. 零基础入门Linux
  2. 使用科大讯飞提供的SDK实现android语音识
  3. Android集成Zxing实现二维码扫描识别,过程
  4. Android的Master/Detail风格界面中实现自
  5. 获取Android设备的唯一识别码|设备号|序号|U
  6. 【Android(安卓)开发教程】显示复杂对话
  7. Android学习笔记(十六)进度条
  8. Android应用框架之AndroidApplication
  9. Android(安卓)2.bootloader编译简析
  10. Android如何获取屏幕的分辨率