您的位置:首页 > 职场人生

黑马程序员-----隐藏应用图标,显示快捷方式,动态定义图标

2014-09-10 11:07 411 查看
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

今天遇到了很无语很坑的需求,说是要在应用安装的时候根据参数的不同动态生成桌面图标,这再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。也就是说应用打包好之后几乎是没有办法去改变它的图标的,更别说是动态生成了!
  这个问题今天也是困扰了我很久,真的是找不到做法,后来有人提示了下可以用快捷方式,慢慢的尝试终于能伪装的把这蛋疼的需求实现了。
  首先看看如何为应用创建的快捷方式,删除快捷方式,以及判断某个快捷方式是否已经存在。
//新增快捷方式
private void shortcutAdd(String name, int number) {
//设置快捷方式点击后要打开的Activity(主入口)
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.setAction(Constant.ACTION_PLAY);

//这里创建了一个numbe的bitmap, 也可以设置自己想要的图表
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Paint paint = new Paint();
paint.setColor(0xFF808080); // gray
paint.setTextAlign(Paint.Align.CENTER);
paint.setTextSize(50);
new Canvas(bitmap).drawText(""+number, 50, 50, paint);

//设置快捷方式
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);

//创建快捷方式
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
//删除快捷方式
private void shortcutDel(String name) {
// Intent to be send, when shortcut is pressed by user ("launched")
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.setAction(Constant.ACTION_PLAY);

// Decorate the shortcut
Intent delIntent = new Intent();
delIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
delIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);

// Inform launcher to remove shortcut
//删除快捷方式
delIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(delIntent);
}

//判断快捷方式是否存在
public boolean isAddShortCut() {

final ContentResolver cr = this.getContentResolver();

int versionLevel = android.os.Build.VERSION.SDK_INT;
String AUTHORITY = "com.android.launcher2.settings";

//2.2以上的系统的文件文件名字是不一样的
if (versionLevel >= 8) {
AUTHORITY = "com.android.launcher2.settings";
} else {
AUTHORITY = "com.android.launcher.settings";
}

final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/favorites?notify=true");
Cursor c = cr.query(CONTENT_URI,
new String[] { "title", "iconResource" }, "title=?",
new String[] { getString(R.string.app_name) }, null);

if (c != null && c.getCount() > 0) {
return true;
}
return false;
}


不要忘记加相应的权限设置:
<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 这里有很好的说明,感兴趣看看。

这样基本上能实现那种要根据参数设置图标的需求,但是这样一来应用卸载的时候就比较麻烦了,因为找不到图标,只能有相应的管理软件来卸载了,有点流氓 ...

处理的时候如果只是隐藏图标 也是就是删除
<category android:name="android.intent.category.LAUNCHER" />
这样程序就没了入口,装了等于没装,大家在研究试试吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: