[Android]_ch11.1_Notification通告訊息元件1
npublic static PendingIntent getActivity(Context context, int requestCode,Intent intent, int
flags)
flags)
ncontext:上下文对象。
nrequstCode:请求码,发件人的私人请求代码(当前未使用)。
nintent:请求意图。用于要指明要启动的类以及数据的传递;
nflags:这是一个关键的标志位:
FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。
FLAG_NO_CREATE:如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null。
FLAG_ONE_SHOT:该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。
FLAG_UPDATE_CURRENT:如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。
package com.derek.ch27_notification;
import android.R.integer;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewDebug.FlagToString;
import android.widget.Button;
public class MainActivity extends Activity {
Button btn1,btn2;
NotificationManager nm;
int id1=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Notification notification=new Notification(R.drawable.icon1, "巨匠電腦課程異動通知", System.currentTimeMillis());
PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, 0, MainActivity.this.getIntent(), PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(MainActivity.this, "巨匠電腦課程異動通知", "Android手機假日班停課一次!", pendingIntent);
notification.defaults=Notification.DEFAULT_SOUND;
notification.defaults=Notification.DEFAULT_LIGHTS;
notification.defaults=Notification.DEFAULT_VIBRATE;
notification.vibrate=new long[]{100,500,100,500};
nm.notify(id1, notification);
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nm.cancel(id1);
}
});
}
}
沒有留言:
張貼留言