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  这里有很好的说明,感兴趣看看。

 

posted @ 2014-09-10 11:03  Mauiie_娢  阅读(3557)  评论(1编辑  收藏  举报