Skip to content

Instantly share code, notes, and snippets.

@xianminx
Created July 30, 2013 06:37
Show Gist options
  • Save xianminx/6110783 to your computer and use it in GitHub Desktop.
Save xianminx/6110783 to your computer and use it in GitHub Desktop.
update app shortcut icon on android desktop
package com.example.dynamicappicon;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
installShortCut();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void installShortCut(){
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
// 是否可以有多个快捷方式的副本,参数如果是true就可以生成多个快捷方式,如果是false就不会重复添加
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, generatorContactCountIcon(((BitmapDrawable)(getResources().getDrawable(R.drawable.ic_launcher))).getBitmap()));
shortcutIntent.putExtra("duplicate", false);
Intent mainIntent = new Intent(Intent.ACTION_MAIN);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// 要删除的应用程序的ComponentName,即应用程序包名+activity的名字
//intent2.setComponent(new ComponentName(this.getPackageName(), this.getPackageName() + ".MainActivity"));
mainIntent.setClass(this, this.getClass());
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mainIntent);
//shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, generatorContactCountIcon(((BitmapDrawable)(getResources().getDrawable(R.drawable.ic_launcher))).getBitmap()));
sendBroadcast(shortcutIntent);
}
private Bitmap generatorContactCountIcon(Bitmap icon){
//初始化画布
int iconSize=(int)getResources().getDimension(android.R.dimen.app_icon_size);
Bitmap contactIcon=Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888);
Canvas canvas=new Canvas(contactIcon);
//拷贝图片
Paint iconPaint=new Paint();
iconPaint.setDither(true);//防抖动
iconPaint.setFilterBitmap(true);//用来对Bitmap进行滤波处理,这样,当你选择Drawable时,会有抗锯齿的效果
Rect src=new Rect(0, 0, icon.getWidth(), icon.getHeight());
Rect dst=new Rect(0, 0, iconSize, iconSize);
canvas.drawBitmap(icon, src, dst, iconPaint);
//在图片上创建一个覆盖的联系人个数
int contacyCount=11;
//启用抗锯齿和使用设备的文本字距
Paint countPaint=new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DEV_KERN_TEXT_FLAG);
countPaint.setColor(Color.RED);
countPaint.setTextSize(20f);
countPaint.setTypeface(Typeface.DEFAULT_BOLD);
canvas.drawText(String.valueOf(contacyCount), iconSize-18, 25, countPaint);
return contactIcon;
}
}
@xianminx
Copy link
Author

requires permission

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

@xianminx
Copy link
Author

@xianminx
Copy link
Author

MIUI 上不能安装shortcut

@xianminx
Copy link
Author

MIUI 上不能安装shortcut

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment