使用步骤:
1)创建自定义视图
2)获取远程视图对象(注:Notification的contentView不能为空)
3)设置PendingIntent(来响应各种事件)
4)发起Notification
大体4步骤这里就不详细说了,下面就把DEMO中的列子拿出来说下
1 Activity 中的代码
/** * Notification的使用 * @author root * */ public class NotificationActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notification); Button btn1 = (Button) findViewById( R.id.btn1); btn1.setOnClickListener( new BtnListener()); Button btnCostom2 = (Button) findViewById( R.id.btn_costom2); btnCostom2.setOnClickListener( new BtnListener()); } class BtnListener implements OnClickListener{ @Override public void onClick(View v) { Integer id = v.getId(); switch ( id ) { case R.id.btn1: notifition1(); break; case R.id.btn_costom2: showCustomizeNotification(); break; default: break; } } } /** * Android 默认的notification */ private void notifition1(){ Bitmap btm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( NotificationActivity.this).setSmallIcon(R.drawable.ic_launcher) .setContentTitle("new message title") .setContentText("[email protected]内容的主体"); mBuilder.setTicker("New message");//第一次提示消息的时候显示在通知栏上 mBuilder.setNumber(12); mBuilder.setLargeIcon(btm); mBuilder.setAutoCancel(true);//自己维护通知的消失 //构建一个Intent,点击通知栏,则跳转到 MainActivity上 Intent resultIntent = new Intent(NotificationActivity.this, MainActivity.class); //封装一个Intent PendingIntent resultPendingIntent = PendingIntent.getActivity( NotificationActivity.this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); // 设置通知主题的意图 mBuilder.setContentIntent(resultPendingIntent); //获取通知管理器对象 NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(0, mBuilder.build()); } /** * 自定义 Notification的布局 */ private void showCustomizeNotification(){ CharSequence title = "i am new"; int icon = R.drawable.ic_launcher; long when = System.currentTimeMillis(); Notification noti = new Notification(icon, title, when + 10000); noti.flags = Notification.FLAG_INSISTENT; // 1、创建一个自定义的消息布局 view.xml // 2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段 RemoteViews remoteView = new RemoteViews(this.getPackageName(),R.layout.notification_costom); remoteView.setImageViewResource(R.id.image, R.drawable.ic_launcher); remoteView.setTextViewText(R.id.text , "通知类型为:自定义View"); noti.contentView = remoteView; // 3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法) //这儿点击后简单启动Settings模块 (Activity) PendingIntent contentIntent = PendingIntent.getActivity (NotificationActivity.this, 0,new Intent("android.settings.SETTINGS"), 0); noti.contentIntent = contentIntent; NotificationManager mnotiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mnotiManager.notify(1, noti); } }
2 Activity 对应的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通知1" /> <Button android:id="@+id/btn_costom2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通知2" /> </LinearLayout>
3 自定义的notification的布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_toRightOf="@+id/image" android:layout_height="wrap_content" android:textColor="#000" /> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_toRightOf="@+id/image" android:layout_height="wrap_content" android:textColor="#000" android:text="点击跳转"/> <ProgressBar android:id="@+id/progress_horizontal" style="?android:attr/progressBarStyleHorizontal" android:layout_below="@+id/text" android:layout_toRightOf="@+id/image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" android:progress="50" android:secondaryProgress="75" /> </RelativeLayout>
附件是网上大神写的例子,弄过来做个备份以后好用。