关键代码:
简单的通知的实现。
@SuppressLint("NewApi")
private void showNotification() {
Notification.Builder builder = new Builder(this);
// new Notification.Builder(this)
builder.setContentTitle("New mail from " + "sender.toString()")
.setContentText("subject").setSmallIcon(R.drawable.ic_launcher)
.build();
// .setLargeIcon(aBitmap)
Intent resultIntent = new Intent(this, MyOtherActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("tag", 100);
resultIntent.putExtras(bundle);
// Because clicking the notification opens a new ("special") activity,
// there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
// Sets an ID for the notification
int mNotificationId = 100;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, builder.build());
}
自定义view的通知:
@SuppressLint("NewApi")
private void initNotification() {
Notification.Builder builder = new Builder(this);
// new Notification.Builder(this)
builder.setContentTitle("New mail from " + "sender.toString()")
.setContentText("subject").setSmallIcon(R.drawable.ic_launcher)
.build();
// .setLargeIcon(aBitmap)
Intent resultIntent = new Intent(this, MyOtherActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("tag", 100);
resultIntent.putExtras(bundle);
// Because clicking the notification opens a new ("special") activity,
// there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// 封装自定义的布局
RemoteViews mRemoteViews = new RemoteViews(this.getPackageName(),
R.layout.notification_bar);
// 如果是动态设置值的时候,需要这些操作
mRemoteViews
.setImageViewResource(R.id.img_user, R.drawable.ic_launcher);
mRemoteViews.setTextViewText(R.id.tv, "XXX");
mRemoteViews.setProgressBar(R.id.progressBar, 100, 50, false);
// 点击ImageView时跳转
mRemoteViews
.setOnClickPendingIntent(R.id.img_user, resultPendingIntent);
// builder.setContentIntent(resultPendingIntent);
builder.setContent(mRemoteViews);
// Sets an ID for the notification
int mNotificationId = 100;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, builder.build());
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/img_user"
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignTop="@+id/img_user"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:layout_toLeftOf="@+id/btn"
android:layout_toRightOf="@+id/img_user"
android:text="自定义通知栏样式"
android:textSize="15sp" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignTop="@+id/img_user"
android:text="查看详情" />
<ProgressBar
android:id="@+id/progressBar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/img_user"
android:layout_margin="10dip"
android:layout_toRightOf="@+id/img_user"/>
</RelativeLayout>
通知跳转的 activity、
package com.example.mynotificationintentput;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MyOtherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
int tagId = bundle.getInt("tag");
NotificationManager nm = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Log.e("OtherActivity", "tag = tagId = "+tagId);
nm.cancel(tagId);
}
// @Override
// protected void onNewIntent(Intent intent) {
// // TODO Auto-generated method stub
// super.onNewIntent(intent);
//
// setIntent(intent);
// }
}
下载地址:http://download.csdn.net/detail/flyingsir_zw/9527940