如图:一个是自定义的,一个是调用系统的
自定义Notification:
public void locationNotification(Class<?> cls, int requestCode, String tickerText,
CharSequence contentTitle, CharSequence contentText) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
// 设置statusbar显示的icon
notification.icon = R.drawable.icon;
// 设置statusbar显示的文字信息
// myNoti.tickerText= new_msg ;
notification.flags = Notification.FLAG_AUTO_CANCEL;
// 设置notification发生时同时发出默认声音
notification.defaults = Notification.DEFAULT_SOUND;
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.location_notification);
Bitmap bitmap = null;
// 此处是关键地方,可以从网络或是sdcard上获取图片,转成bitmap就可以
bitmap = drawableToBitmap(this.getResources().getDrawable(R.drawable.icon));
contentView.setImageViewBitmap(R.id.notification_icon, bitmap);
contentView.setTextViewText(R.id.app_name, contentTitle);
contentView.setTextViewText(R.id.notification_content, contentText);
final Calendar mCalendar = Calendar.getInstance();
int mHour = mCalendar.get(Calendar.HOUR_OF_DAY);
int mMinuts = mCalendar.get(Calendar.MINUTE);
contentView.setTextViewText(R.id.notification_time, mHour + ":" + mMinuts);// 当前时间
notification.contentView = contentView;
Intent intent = new Intent(this, cls);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = contentIntent;
// 显示Notification
Random random = new Random(new Date().getTime());
manager.notify(random.nextInt(1000000), notification);
}
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#EE171717"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="5dp" >
<ImageView
android:id="@+id/notification_icon"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_gravity="center_vertical|left"
android:layout_margin="5dp"
android:src="@drawable/icon" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="notification 图片改变 "
android:textColor="#FFFFFFFF"
android:textSize="18sp" />
<TextView
android:id="@+id/notification_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=".... "
android:textSize="14sp" />
</LinearLayout>
<TextView
android:id="@+id/notification_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:text="11:26" />
</LinearLayout>
使用系统自带的:
private void myNotification(Class<?> cls, String tickerText, CharSequence contentTitle,
CharSequence contentText) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
Intent intent = new Intent(this, cls);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pandingIntent = PendingIntent.getActivity(this, 1, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults = Notification.DEFAULT_SOUND;
notification.icon = R.drawable.ic_launcher;// 图标
notification.tickerText = tickerText;
notification.setLatestEventInfo(this, contentTitle, contentText, pandingIntent);
manager.notify(1, notification);
}
项目下载: http://download.csdn.net/detail/u010411826/8293525