zoukankan      html  css  js  c++  java
  • 创建桌面快捷图标

    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" />
  • 相关阅读:
    python网络编程,requests模块
    python操作excel
    python加密模块hashlib
    python操作redis
    python操作mysql
    python常用模块3(os和sys模块)
    python打开网站
    python常用模块2
    python模块简介
    mac下开发——环境心得(杂项,持续性更新)
  • 原文地址:https://www.cnblogs.com/xiaosw/p/3525767.html
Copyright © 2011-2022 走看看