• 创建桌面快捷图标


    1、查询快捷图标是否已被创建

    private boolean isInstallShortcut() {
            // TODO Auto-generated method stub
            //获取当前使用sdk的版本
            int sdkInt = android.os.Build.VERSION.SDK_INT;
            if(sdkInt >= 8) {
                AUTHORITY = "com.android.launcher2.settings";
            } else {
                AUTHORITY = "com.android.launcher.settings";
            }
            Uri uri = Uri.parse("content://" + AUTHORITY + "/favorites");
            String[] projection = new String[]{"title"};
            String selection = "title = ?";
            String[] selectionArgs = new String[]{"new shortcut"};
            Cursor query = getContentResolver().query(uri, projection, selection, selectionArgs, null);
            
            return query.moveToNext();
        }

    2、创建快捷图标

    public void installShortcut() {
         // 查询是否已被创建
    if(isInstallShortcut()) { Toast.makeText(this, "该快捷方式已创建", 0).show(); } else { Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); //设置快捷方式的名称 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "new shortcut"); //快键方式的图标 ShortcutIconResource icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon); Intent myIntent = new Intent(); //设置一个主入口 myIntent.setAction("android.intent.action.MAIN"); myIntent.addCategory("android.intent.category.LAUNCHER"); //跳转至指定的activity ComponentName component = new ComponentName(this, OtherActivity.class); myIntent.setComponent(component); //跳转意图 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent); sendBroadcast(intent); } }

    3、删除快捷图标

    public void unInstallShortcut() {
            if(!isInstallShortcut()) {
                 Toast.makeText(this, "该快捷方式已被卸载或不存在", 0).show();
            } else {
    //            System.out.println("-------------------------unInstall");
                Intent intent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
                //指定快捷方式的名称
                intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "new shortcut");
                
                Intent myIntent = new Intent();
                //设置一个主入口
                myIntent.setAction("android.intent.action.MAIN");
                myIntent.addCategory("android.intent.category.LAUNCHER");
                //跳转至指定的activity
                ComponentName component = new ComponentName(this, MainActivity.class);
                myIntent.setComponent(component);
                intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);
                sendBroadcast(intent);
            }
        }

    4、添加使用权限

    <!-- 创建Shortcut权限 -->
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <!-- 删除Shortcut权限 -->
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
    <!-- 读取设置配置权限 -->
    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
  • 相关阅读:
    二维数组展示到DataGridView(c#)
    发送请求获取响应内容(c#)
    重建freescale 4.6.2 multilib toolchain
    [raspberry pi3] raspberry 充当time machine
    sublime ctags
    lua遍历文件
    pthread中如何追踪stack over flow
    Core Dump
    2 plan team 服务器搭建
    mac上编译 arm linux gnueabi交叉编译工具链toolchain
  • 原文地址:https://www.cnblogs.com/xiaosw/p/3525767.html
Copyright © 2020-2023  润新知