2011.09.13(3)——— android 添加快捷方式并

系统 1883 0
2011.09.13(3)——— android 添加快捷方式并且图标上添加数字

前两个日志的合并

我们可以给桌面添加快捷方式的时候 制定一个带数字的图片

2011.09.13(3)——— android 添加快捷方式并且图标上添加数字


1、添加一个方法

    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;
    }
  


上面
    int contacyCount=11;
  

这个是写死的 你可以在程序里面更具业务来控制

2、修改方法:

    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("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.icon))).getBitmap()));
		sendBroadcast(shortcutIntent);
	}
  



以前是根据id来找到图片
    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.icon))).getBitmap()));
  


这样既可

但是 这样会不停的删除 创建快捷方式 更新太多
网上貌似大家都是用widget来做的 这个有空可以看一下


2011.09.13(3)——— android 添加快捷方式并且图标上添加数字


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论