Android PendingIntent Notification

首先,我感覺在實現中PendingIntent感覺就是Intent的包裝。

它的三個實例化方法:

getActivity(Context, int, Intent, int)

getService(Context, int, Intent, int)

getBroadcast(Context, int, Intent, int)

感覺是保存當前的Activity的Context,然後在外部啓動Intent動作。類似於代碼Context.startActivity(*, *);

常和Notification和Alarm一起使用。


代碼例子:

public class BannerActivity extends Activity {
	
	private Button b;
	private NotificationManager mNotificationManager;
	private Intent intent;
	private PendingIntent mPendingIntent;
	private Notification mNotification;

    /** Called when the activity is first created. */	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        
        b = (Button)this.findViewById(R.id.b);
        
        intent = new Intent(BannerActivity.this, Activity01.class);
        mPendingIntent = PendingIntent.getActivity(BannerActivity.this, 0, intent, Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        mNotification = new Notification();
        
        b.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mNotification.icon = R.drawable.ic_launcher;
				mNotification.tickerText = "通知!"; //通知在通知欄出現的時候的標題
				mNotification.defaults = Notification.DEFAULT_SOUND; 
				
				// 第二個參數是打開通知欄後的標題, 第三個參數是通知內容
				mNotification.setLatestEventInfo(BannerActivity.this, "通知?", "通知內容!", mPendingIntent);
				mNotificationManager.notify(0, mNotification);
			}
		});
        
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章