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"/>

  • 相关阅读:
    一本通1018
    并查集&MST
    hdu 1875 畅通工程再续
    hdu 1811 Rank of Tetris(拓扑排序+并查集)
    hdu 1325 is it a tree?
    hdu1285拓扑排序
    hdu2063 过山车(最大二分匹配)
    最小生成树二·Kruscal算法
    hiho一下 第二十一周(线段树 离散化)
    hiho一下 第二十周(线段树模板)
  • 原文地址:https://www.cnblogs.com/zyw-205520/p/5251409.html
Copyright © 2011-2022 走看看