在android3.0之后,Android的通知与之前的版本有所区别。为了更好的使用,androidSDK引入了Android.app.Notificition.Builder这个类。这样就与之前使用发生了变化。那就说一下在Android3.0之后Notificaition的使用 import android.annotation.SuppressLint;
import android.app.Activity;import android.app.Notification;
import android.app.Notification.Builder;立即发表
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent; import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.os.Bundle; import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
@SuppressLint("NewApi")
public class MainActivity extends Activity
{
private Button but1= null;
private NotificationManager nm =null;
private PendingIntent contentIntent = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init(){
but1 =(Button)super.findViewById(R.id.but1);
but1.setOnClickListener(onclick);
nm = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this,MainActivity.class);
contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);
}
OnClickListener onclick = new OnClickListener(){
private final int NOTIFICATION_BASE_NUMBER=110;
private Builder builder = null;
private Notification n = null;
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.but1: NotificationManager nm = (NotificationManager) MainActivity.this .getSystemService(NOTIFICATION_SERVICE);
Resources res = MainActivity.this.getResources();
builder = new Notification.Builder(MainActivity.this);
builder.setContentIntent(contentIntent) .
setSmallIcon(R.drawable.ic_launcher)//设置状态栏里面的图标(小图标) .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.i5))//下拉下拉列表里面的图标(大图标) .setTicker("this is bitch!") //设置状态栏的显示的信息
.setWhen(System.currentTimeMillis())//设置时间发生时间
.setAutoCancel(true)//设置可以清除
.setContentTitle("This is ContentTitle")//设置下拉列表里的标题
.setContentText("this is ContentText");//设置上下文内容
n = builder.getNotification();//获取一个Notification
n.defaults =Notification.DEFAULT_SOUND;//设置为默认的声音
nm.notify(NOTIFICATION_BASE_NUMBER, n);//显示通知 break; }
}
};
}
Layout.xml文件我就不发出来了,就一个Button。 当按下主界面的Button后会出现会在下拉列表中出现 This is ContentTitle。
转载说明出处.