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" />
  • 相关阅读:
    Android学习笔记03:学习过程中碰到的一些问题及解决方法
    写于莫言获得诺贝尔文学奖之际
    Windows环境下QT学习笔记01:QT及QT Creator的下载及安装
    Android学习笔记02:AndroidManifest.xml源码
    Android学习笔记01:开发环境搭建
    怀念我的大学四年
    喜获TI MSP430 LaunchPad开发板
    Win7下VS2008破解方法
    手把手教你把Vim改装成一个IDE编程环境
    顺序线性表
  • 原文地址:https://www.cnblogs.com/xiaosw/p/3525767.html
Copyright © 2011-2022 走看看