Android生成桌面快捷方式的几种方法:
//------------以下为动态替换桌面应用Icon的一种解决方案-------------------
// 1.获取本地目录图片的Bitmap ;根据Bitmap绘制新的canvas画布Jicanvas画布上添加文字信息;最终获得一个带有canvas的Bitmap:NewIcon
public Bitmap getBitmap() {
//获取本地bitmap
Bitmap bitmap = BitmapFactory.decodeFile("/scard/1.png");
Bitmap bitmap2 = BitmapFactory.decodeFile("/scard/2.png");
//根据Bitmap绘制新的Canvas画布
Bitmap NewIcon = Bitmap.createBitmap(android.R.dimen.app_icon_size, android.R.dimen.app_icon_size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(NewIcon);
int iconSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
Paint iconPaint = new Paint();
Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
Rect dst = new Rect(0, 0, iconSize, iconSize);
canvas.drawBitmap(bitmap, src, dst, iconPaint);
//w往canvas 画布上添加文字信息
Paint StrPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
StrPaint.setColor(Color.RED);
StrPaint.setTextSize(20f);
StrPaint.setTypeface(Typeface.DEFAULT_BOLD);
canvas.drawText("正面", iconSize - 45, 20, StrPaint);
//最终获得一个带有canvas的Bitmap:NewIcon
return NewIcon;
}
//2.使用新的Bitmap在Home界面创建制定应用的启动项
public void setHomeIcon() {
Intent shortcutIntent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MaginIcon");
Intent mainIntent = new Intent(Intent.ACTION_MAIN);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mainIntent.setClass(this, MainActivityBak.class);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mainIntent);
sendBroadcast(shortcutIntent);
}
public void setIcons() {
//创建新的启动项
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MagicsIcon");
//设置不可以创建多个启动项
shortcutIntent.putExtra("duplicate", false);
//创建Home界面启动项
Intent mainIntent = new Intent(Intent.ACTION_MAIN);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mainIntent.setClass(this, MainActivityBak.class);
PackageManager pkgMag = getPackageManager();
Intent queryIntent = new Intent(Intent.ACTION_MAIN, null);
queryIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// List<ResolveInfo> list = pkgMag.queryIntentActivities(queryIntent,PackageManager.GET_ACTIVITIES);
//
// for(int i = 0;i < list.size();i++){
// ResolveInfo info = list.get(i);
// if(info.activityInfo.packageName.equals(pk)){
//
// }
// }
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mainIntent);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, getBitmap());
sendBroadcast(shortcutIntent);
}
//------------以下为动态替换桌面应用Icon的一种解决方案-------------------
//------------以下为生成桌面widget的几种方法,本质一样----------------
private void shortcutAdd(String name, int number) {
// Intent to be send, when shortcut is pressed by user ("launched")
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivityBak.class);
//shortcutIntent.setAction(SyncStateContract.Constants.ACCOUNT_NAME);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Create bitmap with number in it -> very default. You probably want to give it a more stylish look
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
paint.setColor(0xFFFF0000); // gray
paint.setTextAlign(Paint.Align.CENTER);
paint.setTextSize(50);
new Canvas(bitmap).drawText("" + number, 50, 50, paint);
((ImageView) findViewById(R.id.icon)).setImageBitmap(bitmap);
// Decorate the shortcut
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);
// Inform launcher to create shortcut
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(), MainActivityBak.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 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 void addShortCut() {
Intent myLauncherIntent = new Intent(this, MainActivityBak.class);
myLauncherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myLauncherIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MagicIcon");
intent.putExtra
(
Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext
(
getApplicationContext(),
R.mipmap.ic_launcher
)
);
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent);
}