Android入門之通知欄

□ 首先,發送一個狀態欄通知必須用到兩個類:NotificationManager、Notification。

NotificationManager:狀態欄通知的管理類,負責發通知、清楚通知等。

它必須通過getSystemService()方法來獲取:

NotificationManagernm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

 Notification:是具體的狀態欄通知對象,可以設置icon、文字、提示聲音、振動等等參數。

 

□ 設置一個Notification對象的基本參數:

icon  (通知的圖標)

title and expanded message  (通知的標題和內容)

PendingIntent   (點擊通知執行頁面跳轉)

 

可選的設置:

ticker-text message (狀態欄頂部提示消息)

alert sound    (提示音)

vibrate setting  (振動)

flashing LED setting (燈光)等等

 

Notificationnotification = new Notification(

    R.drawable.ic_launcher,//圖標

    "Reminder:Meeting starts in 5minutes",//滾動文本

    System.currentTimeMillis());    //獲取當前的時間

 

□ 給Notification對象添加屬性

//添加震動

notification.defaults |=Notification.DEFAULT_VIBRATE;

//添加聲音

notification.defaults |= Notification.DEFAULT_SOUND;

//添加LED燈閃爍

notification.defaults |= Notification.DEFAULT_LIGHTS;

 

□ Notification附加屬性flags:

//重複發出聲音,直到用戶響應此通知

notification.flags |=Notification.FLAG_INSISTENT;

//在通知欄上點擊此通知後自動清除此通知

notification.flags |=Notification.FLAG_AUTO_CANCEL;

//將此通知放到通知欄的"Ongoing"即"正在運行"組中

notification.flags |=Notification.FLAG_ONGOING_EVENT;

//表明在點擊了通知欄中的"清除通知"後,此通知不清除 

notification.flags |=Notification.FLAG_NO_CLEAR;

 

□ 更新Notification對象

CharSequencecontentTitle = "警告";

CharSequencecontentText = "請刪除你手機裏面的360軟件";

NotificationManagermanager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

Intentintent = new Intent(MainActivity.this, MyNotification.class);

intent.putExtra("notificationID",NOTIFICATION_ID);

PendingIntentpending = PendingIntent.getActivity(this, 0, intent, 0);   

notification.setLatestEventInfo(this,contentTitle, contentText, pending);

manager.notify(NOTIFICATION_ID,notification);

如果想要更新一個通知,只需要在設置好notification之後,再次調用 setLatestEventInfo(),然後重新發送一次通知即可,即再次調用notify()。

 

□ 在MyNotification.java類中取消通知

NotificationManagernm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

nm.cancel(getIntent().getExtras().getInt(“notificationID”));

 

□ AndroidManifest.xml文件修改

聲明activity:

<activityandroid:name=".MyNotification"></activity>

獲取震動權限:

<uses-permissionandroid:name="android.permission.VIBRATE"/>

 

下面看我實現的代碼:寫代碼的一個原則是不要把一個方法一個類寫得太冗長,所以我根據上面的劃分:

1)首先創建一個類MyNotification.java和一個notification.xml,xml文件中創建一個TextView,寫點字上去,太簡單了就不說了,MyNotification.java代碼如下:

 

public class MyNotification extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_notification);
		
		NotificationManager manager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		manager.cancel(getIntent().getExtras().getInt("notificationID"));
	}
}

2)然後在AndroidManifest.xml文件中,聲明MyNotification活動:

 

 <activity android:name=".MyNotification"></activity>

3)在MainActivity中的代碼如下

(注:import的內容不寫出來時因爲在eclipse裏面按ctrl+shift+字母O就可以自動導入了,沒有特殊的我不特意寫出來)

 

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
	public final int NOTIFICATION_ID = 1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	public void onClickNotification(View v){
		updataNotification(myNotification());
	}
	
	protected Notification myNotification(){
		Notification notification = new Notification(
				R.drawable.ic_launcher,
				"系統檢測到你的手機裝了360軟件,請儘快刪除!",
				System.currentTimeMillis());
		//添加震動
		notification.defaults |= Notification.DEFAULT_VIBRATE;
		//添加聲音
		notification.defaults |= Notification.DEFAULT_SOUND;	
		return notification;	
	}
	protected void updataNotification(Notification notification){	
		CharSequence contentTitle = "警告";
		CharSequence contentText = "請刪除你手機裏面的360軟件";
		NotificationManager manager 
			= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		//點擊該通知後要跳轉的Activity
		Intent intent = new Intent(MainActivity.this, MyNotification.class); 
		intent.putExtra("notificationID", NOTIFICATION_ID);
		PendingIntent pending = PendingIntent.getActivity(this, 0, intent, 0);	
		notification.setLatestEventInfo(this, contentTitle, contentText, pending);
		manager.notify(NOTIFICATION_ID,notification);
	}
}

當然activity_main.xml文件中創建了一個Button,太簡單了就不說了。

好累啊,今天一整天都在寫博客,把一段時間內的複習總結。

 

 


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