创建快捷方式代码:
public static final String READ_SETTINGS_PERMISSION = "com.Android.launcher.permission.READ_SETTINGS";
public static final String INSTALL_SHORTCUT_PERMISSION = "com.android.launcher.action.INSTALL_SHORTCUT";
private static final String TAG = "WowUtils";
/**
* 创建桌面快捷方式
* @param context
* @param 点击快捷方式进入的Activity
* @param title 快捷方式显示名
* @param iconRes 快捷方式图标的resource id
*/
public static void createShortcut(Context context,Class activity,String title,int iconRes){
if(context == null || activity == null || isShortcutExist(context)){
return ;
}
Intent addIntent = new Intent(INSTALL_SHORTCUT_PERMISSION);
Parcelable icon = Intent.ShortcutIconResource.fromContext(context,iconRes);// 获取快捷键的图标
addIntent.putExtra("duplicate", false);
Intent myIntent = new Intent(context,activity);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,title);// 快捷方式的标题
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 快捷方式的图标
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);// 快捷方式的动作
context.sendBroadcast(addIntent);
return ;
}
public static boolean isShortcutExist(Context context) {
String shortcutTitle = getShortcutTitle(context);
Log.i(TAG, "getShortcutTitle=" + shortcutTitle);
return !TextUtils.isEmpty(shortcutTitle);
}
public static String getShortcutTitle(Context context){
if(context == null){
return null;
}
String appLabel = null;
try {
PackageManager pm = context.getPackageManager();
appLabel = pm.getApplicationLabel(pm.getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA)).toString();
} catch (Exception e) {
return null;
}
String authority = getAuthorityFromPermission(context,READ_SETTINGS_PERMISSION);
Log.i(TAG, "getAuthorityFromPermission=" + authority);
final String uriStr = "content://" + authority + "/favorites?notify=true";
final Uri uri = Uri.parse(uriStr);
final Cursor c = context.getContentResolver().query(uri,
new String[] { "title" }, "title=?", new String[] { appLabel },
null);
if (c != null && c.getCount() > 0) {
c.moveToFirst();
do{
return c.getString(c.getColumnIndexOrThrow("title"));
}while(c.moveToNext());
}
return null;
}
/**
* The launcher is an Application under the Handset Manufacturer
* responsibility. The Authority is then not always
* "com.android.launcher2.settings". The Handset Manufacturer may rewrite
* its own. It can be "com.android.twlauncher" or anything else depending on
* the Java package. You need to retrieve the right authority by searching
* for a provider that declares the read/write permissions
* "com.android.launcher.permission.READ_SETTINGS" or
* "com.android.launcher.permission.WRITE_SETTINGS".
*
* @param context
* @param permission
* @return e.g. com.baidu.launcher2.settings
*/
public static 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;
}
Manifest.xml添加权限: