zoukankan      html  css  js  c++  java
  • android 隐藏应用图标,用快捷方式做启动入口,实现伪动态改变图标

         今天遇到了很无语很坑的需求,说是要在应用安装的时候根据参数的不同动态生成桌面图标,这再android里基本上是不可能的,下面有stackoverflow上的一句话:

    You cannot change the application icon (or the Android manifest, or any other application resource for that matter) once your APK is compiled. The only way to do this is by re-compiling and pushing an update to the market。也就是说应用打包好之后几乎是没有办法去改变它的图标的,更别说是动态生成了!

      这个问题今天也是困扰了我很久,真的是找不到做法,后来有人提示了下可以用快捷方式,慢慢的尝试终于能伪装的把这蛋疼的需求实现了。

      首先看看如何为应用创建的快捷方式,删除快捷方式,以及判断某个快捷方式是否已经存在。

      

     1 //新增快捷方式
     2     private void shortcutAdd(String name, int number) {
     3             //设置快捷方式点击后要打开的Activity(主入口)
     4             Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
     5             shortcutIntent.setAction(Constant.ACTION_PLAY);
     6 
     7             //这里创建了一个numbe的bitmap, 也可以设置自己想要的图表
     8             Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher); 
     9             Paint paint = new Paint();
    10             paint.setColor(0xFF808080); // gray
    11             paint.setTextAlign(Paint.Align.CENTER);
    12             paint.setTextSize(50);
    13             new Canvas(bitmap).drawText(""+number, 50, 50, paint);
    14 
    15             //设置快捷方式
    16             Intent addIntent = new Intent();
    17             addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    18             addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    19             addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
    20 
    21             //创建快捷方式
    22             addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    23             getApplicationContext().sendBroadcast(addIntent);
    24         }
    25         //删除快捷方式
    26         private void shortcutDel(String name) {
    27             // Intent to be send, when shortcut is pressed by user ("launched") 
    28             Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
    29             shortcutIntent.setAction(Constant.ACTION_PLAY);
    30 
    31             // Decorate the shortcut
    32             Intent delIntent = new Intent();
    33             delIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    34             delIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    35             
    36             // Inform launcher to remove shortcut
    37             //删除快捷方式
    38             delIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
    39             getApplicationContext().sendBroadcast(delIntent);
    40         }
    41         
    42         //判断快捷方式是否存在
    43         public boolean isAddShortCut() {
    44 
    45             final ContentResolver cr = this.getContentResolver();
    46 
    47             int versionLevel = android.os.Build.VERSION.SDK_INT;
    48             String AUTHORITY = "com.android.launcher2.settings";
    49             
    50             //2.2以上的系统的文件文件名字是不一样的
    51             if (versionLevel >= 8) {
    52                 AUTHORITY = "com.android.launcher2.settings";
    53             } else {
    54                 AUTHORITY = "com.android.launcher.settings";
    55             }
    56 
    57             final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
    58                     + "/favorites?notify=true");
    59             Cursor c = cr.query(CONTENT_URI,
    60                     new String[] { "title", "iconResource" }, "title=?",
    61                     new String[] { getString(R.string.app_name) }, null);
    62 
    63             if (c != null && c.getCount() > 0) {
    64                 return true;
    65             }
    66             return false;
    67         }

    不要忘记加相应的权限设置:

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

    以上就是如何为应用创建、删除应用快捷方式了,但是需求上是想要动态设置图标,仅仅添加快捷方式的话,原来的应用图标还是在的,我们也可以设置应用图标不显示的。

    一下就是如何隐藏应用图标

    只要在清单文件中的<intent-filter>的节点下设置

     <data android:host="MainActivity" android:scheme="com.android.example" /> 即可。
     <activity
                android:name="com.longtime.ajy.MainActivity"
                android:screenOrientation="portrait"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <!--data android:host="MainActivity" android:scheme="com.android.example" /  -->
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>

     关于为什么这样设置就不显示图标:http://blog.csdn.net/xiazdong/article/details/7764865  这里有很好的说明,感兴趣看看。

  • 相关阅读:
    ggplot2绘图入门系列之二:图层控制与直方图
    机器学习与数据挖掘中的十大经典算法
    mysql使用存储过程执行定时任务
    使用hbase-shaded-client解决google包冲突问题
    vue 表单校验及气泡清除
    druid配置
    如何修改maven jar包源码
    jar包冲突最新解决方式
    Hive安装
    Hbase
  • 原文地址:https://www.cnblogs.com/mauiie/p/3963019.html
Copyright © 2011-2022 走看看