zoukankan      html  css  js  c++  java
  • 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"/>

  • 相关阅读:
    JavaScript入门知识点整理
    正则表达式
    bootstrap css编码规范
    JavaScript高级编程(学习笔记)
    【 D3.js 选择集与数据详解 — 2 】 使用data()绑定数据
    bootstrap table:JQuery中each方法绑定blur事件监听input输入是否合法,进入死循环
    bootstrap-table中导出excel插件bootstrap-table-export使用
    托业考后感
    《Pride and Prejudice》英文版读后记忆
    迷茫的当下,我在做什么
  • 原文地址:https://www.cnblogs.com/zyw-205520/p/5251409.html
Copyright © 2011-2022 走看看