Android中的桌面快捷方式

时间:2023-03-10 03:33:59
Android中的桌面快捷方式

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

    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>