Android中的桌面快捷图标

Android中很多App都有快捷图标,在这儿记录一下建立快捷图标的方法,

app快捷图标的建立,记得这里有权限问题

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

代码

 public class DeskMapUtil {

public static void createShortCut(Context context) {
    // 先判断该快捷是否存在
    if (!isExist(context)) {
        Intent intent = new Intent();
        // 指定动作名称
        intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        // 指定快捷方式的图标
        Parcelable icon = Intent.ShortcutIconResource.fromContext(context,R.mipmap.tool_icon);
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
        // 指定快捷方式的名称
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Matrix");
        // 指定快捷图标激活哪个activity
        Intent i = new Intent();
        i.setAction(Intent.ACTION_MAIN);
        i.addCategory(Intent.CATEGORY_LAUNCHER);
        ComponentName component = new ComponentName(context, SplashActivity.class);
        i.setComponent(component);
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, i);
        context.sendBroadcast(intent);
    }
}
private static boolean isExist(Context context) {
    boolean isExist = false;
    int version = getSdkVersion();
    Uri uri = null;
    if (version < 2.0) {
        uri = Uri.parse("content://com.android.launcher.settings/favorites");
    } else {
        uri = Uri.parse("content://com.android.launcher2.settings/favorites");
    }
    String selection = " title = ?";
    String[] selectionArgs = new String[] { "Matrix" };
    Cursor c = context.getContentResolver().query(uri, null, selection, selectionArgs, null);

    if (c != null && c.getCount() > 0) {
        isExist = true;
    }else {
        isExist=false;
    }

    if (c != null) {
        c.close();
    }

    return isExist;
}
/**
 * 得到当前系统SDK版本
 */
private static int getSdkVersion() {
    return android.os.Build.VERSION.SDK_INT;
}
    原文作者:不识水的鱼
    原文地址: https://www.jianshu.com/p/458a62ce3cd6
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞