• Android中为你的应用程序添加桌面快捷方式


    
    
    public void ShortCut(View view){
            createDeskShortCut(this,getString(R.string.short_cut),R.drawable.ups_icon,"http://www.baidu.com");
            createDeskShortCut(this,getString(R.string.short_cut2),R.drawable.ups_icont,"http://image.baidu.com/");
            Toast.makeText(this, "创建完成", 1).show();
        }
         /**
         * 创建快捷方式
         */
        public void createDeskShortCut(Context context,String name,int iconRes,String url) {
    
               Log.i("Javen","------createShortCut--------");
               //创建快捷方式的Intent
               Intent shortcutIntent = new Intent(
                             "com.android.launcher.action.INSTALL_SHORTCUT");
               //不允许重复创建
               shortcutIntent.putExtra("duplicate",false);
               //需要现实的名称
               shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    
               //快捷图片
               Parcelable icon = Intent.ShortcutIconResource.fromContext(
                             getApplicationContext(),iconRes);
    
               shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
    
    //           Intent intent = new Intent(getApplicationContext(),
    //                         MainActivity.class);
               Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
               //下面两个属性是为了当应用程序卸载时桌面 上的快捷方式会删除
               intent.setAction("android.intent.action.MAIN");
               intent.addCategory("android.intent.category.LAUNCHER");
               //点击快捷图片,运行的程序主入口
               shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);
               //发送广播。OK
               sendBroadcast(shortcutIntent);
        }

         当你卸装你的应用程序的时候你又会发现存在一个问题就是你的应用程序虽然卸载了,可是桌面上的快捷方式并未卸载。呵呵,其实你只要在你要设置对应启动进入的那个Intent加上这么下面的两个属性就是表明与你的应用绑定了。

         

               //下面两个属性是为了当应用程序卸载时桌面 上的快捷方式会删除
               intent.setAction("android.intent.action.MAIN");
               intent.addCategory("android.intent.category.LAUNCHER");

    千万别忘记在AndroidManifest.xml加上下面的这个权限:

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

  • 相关阅读:
    代码题(11)— 链表两数相加
    机器学习(十九)— xgboost初试kaggle
    【js】鼠标跟随效果
    【js】event(事件对象)详解
    【vue】vue +element 搭建项目,this.$nextTick用法
    【vue】vue +element 搭建及开发中项目中,遇到的错误提示
    【vue】vue +element 搭建项目,点击空白处关闭弹窗
    【js】把一个json对象转成想要的数组
    【vue】vue +element 搭建项目,使用el-date-picker组件遇到的坑
    【vue】vue +element 搭建项目,在使用InputNumber 计数器时遇到的问题
  • 原文地址:https://www.cnblogs.com/zyw-205520/p/5251409.html
Copyright © 2020-2023  润新知