安卓通知栏自定义布局提示(NotificationCompat.Builder)

1.activity代码

package com.example.lockscreen;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.RemoteViews;

public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	public void clickNo(View v){
	
	       if(v.getId() == R.id.button1){
	    	     //系统默认布局 
	    	     Bitmap btm = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
	   		     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this).setSmallIcon(R.drawable.ic_launcher)
	   				.setContentTitle("5 new message")
	   				.setContentText("[email protected]");
	   		     
	   				mBuilder.setTicker("New message");//第一次提示消息的时候显示在通知栏上
	   				mBuilder.setNumber(12);
	   				mBuilder.setLargeIcon(btm);
	   				mBuilder.setAutoCancel(true);//自己维护通知的消失
	   			    //使用默认提示音
	 			    mBuilder.setDefaults(Notification.DEFAULT_ALL);
	   			   //点击通知栏跳转的activity
	   				Intent resultIntent = new Intent(MainActivity.this,
	   						MainActivity.class);
	   				PendingIntent resultPendingIntent = PendingIntent.getActivity(
	   						MainActivity.this, 0, resultIntent,
	   						PendingIntent.FLAG_UPDATE_CURRENT);
	   				
	   				mBuilder.setContentIntent(resultPendingIntent);
	   				
	   				NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
	   				mNotificationManager.notify(0, mBuilder.build());
	       }else if(v.getId() == R.id.button2){
	    	   //自定义显示布局
	           RemoteViews contentViews = new RemoteViews(getPackageName(),R.layout.custom_notification);
			   //通过控件的Id设置属性
			   contentViews.setImageViewResource(R.id.imageNo, R.drawable.ic_launcher);
			   contentViews.setTextViewText(R.id.titleNo, "自定义通知标题");
			   contentViews.setTextViewText(R.id.textNo, "自定义通知内容");
			   //点击通知栏跳转的activity
			   Intent intent = new Intent(MainActivity.this, MainActivity.class);
			   PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent,
			   PendingIntent.FLAG_CANCEL_CURRENT);
			   
			   NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
			   MainActivity.this).setSmallIcon(R.drawable.ic_launcher)
			   .setContentTitle("My notification")
			   .setTicker("new message");
			   //自动管理通知栏消息
			   mBuilder.setAutoCancel(true);
			   mBuilder.setContentIntent(pendingIntent);
			   ///自定义布局
			   mBuilder.setContent(contentViews);
			   //使用默认提示音
			   mBuilder.setDefaults(Notification.DEFAULT_ALL);
			   NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
			   mNotificationManager.notify(1, mBuilder.build());
	       }	
	}
}

2.自定义布局文件(custom_notification.xml)



	
	
		
		
			  
			    
		



3.源码下载:http://download.csdn.net/detail/wd4871/9440333


你可能感兴趣的:(android)