AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.skex.notificationiconcount"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.skex.notificationiconcount.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application></manifest>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.skex.notificationiconcount.MainActivity$PlaceholderFragment" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="center" /></LinearLayout>
notification.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp"
>
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000"
/>
</LinearLayout>
MainActivity.java
package com.skex.notificationiconcount;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
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.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RemoteViews;
import android.os.Build;public class MainActivity extends ActionBarActivity {
private static final String TAG = "Main";
private ImageView mImageView;
private NotificationManager nm;
private int NOTIFICATION_CONTACT_ID=111; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView=(ImageView)findViewById(R.id.icon);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//mImageView.setImageBitmap(bitmap);
//boolean a=true;
//if(a)
//return;
//Bitmap icon;
//icon=getResIcon(getResources(), R.id.icon);
//mImageView.setImageBitmap(icon);
//mImageView.setImageBitmap(generatorContactCountIcon(icon));
//mImageView.setImageBitmap(generatorContactCountIcon(bitmap));
Bitmap contactCountIcon=generatorContactCountIcon(bitmap);
nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
showNotifacation(contactCountIcon);
}
/**
* 显示状态栏通知
* @param icon 通知内容图标
*/
private void showNotifacation(Bitmap icon){
//drawable\vvv.png
Notification notification=new Notification(R.drawable.vvv,"联系人数量",System.currentTimeMillis());
//使用RemoteView自定义通知视图
RemoteViews contentView=new RemoteViews(getPackageName(), R.layout.notification);
contentView.setImageViewBitmap(R.id.image, icon);
contentView.setTextViewText(R.id.text, "图标上的红色数字表示手机中联系人的数量");
notification.contentView=contentView;
Intent notificationIntent=new Intent(this, MainActivity.class);
PendingIntent contentIntent=PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent=contentIntent;
nm.notify(NOTIFICATION_CONTACT_ID, notification);
} @Override
protected void onDestroy() {
super.onDestroy();
nm.cancel(NOTIFICATION_CONTACT_ID);
} /**
* 根据id获取一个图片
* @param res
* @param resId
* @return
*/
//private Bitmap getResIcon(Resources res,int resId){
// Drawable icon=res.getDrawable(resId);
// if(icon instanceof BitmapDrawable){
// BitmapDrawable bd=(BitmapDrawable)icon;
// return bd.getBitmap();
// }else{
// return null;
// }
// }
/**
* 获取联系人的个数
* @return 手里通讯录中联系人的个数
*/
private int getContactCount(){
return 9;
} /*
* 在给定的图片的右上角加上联系人数量。数量用红色表示
* @param icon 给定的图片
* @return 带联系人数量的图片
*/
private Bitmap generatorContactCountIcon(Bitmap icon){
//初始化画布
int iconSize=(int)getResources().getDimension(android.R.dimen.app_icon_size);
Log.d(TAG, "the icon size is "+iconSize);
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=getContactCount();
//启用抗锯齿和使用设备的文本字距
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;
}
@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;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
显示状态栏通知
转载上一篇:heredoc
下一篇://array_2d.php
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
将任务栏图标设置为只显示小图标
任务栏图标修改
任务栏 小图标