系統服務-----NotificationManager

熟悉api事例筆記:

package com.test;

import com.example.test.R;

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.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
	private static final int NOTIFICATION_FLAG = 1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	public void notificationMethod(View view) {
		// 在Android進行通知處理,首先需要從系統哪裏獲得通知管理器NotificationManager,它是一個系統Service。
		NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		switch (view.getId()) {
		case R.id.btn1:
			 // 創建一個PendingIntent,和Intent類似,不同的是由於不是馬上調用,需要在下拉狀態條出發的activity,所以採用的是PendingIntent,即點擊Notification跳轉啓動到哪個Activity
			PendingIntent pi = PendingIntent.getActivity(this,0,new Intent(this,MainActivity.class),0);
			Notification notyfi1 = new Notification();
			notyfi1.icon = R.drawable.ic_launcher;
			notyfi1.tickerText = "您有一天新消息請注意查收 ";
			notyfi1.when = System.currentTimeMillis();
			notyfi1.flags |= Notification.FLAG_AUTO_CANCEL;// FLAG_AUTO_CANCEL表明當通知被用戶點擊時,通知將被清除。
			notyfi1.number = 1;
			notyfi1.setLatestEventInfo(this, "標題", "內容", pi);
			//通過通知管理器來發起通知。如果id不同,則每click,在statu那裏增加一個提示
			nm.notify(NOTIFICATION_FLAG,notyfi1);
			break;

		default:
			break;
		}
	}
}

發佈了96 篇原創文章 · 獲贊 23 · 訪問量 69萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章