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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容