因為有需求要做非系統通知,是以小馬找個時間幹脆一起學習了系統預設的通知與自定義通知的實作,吼吼,雖然簡單,但開心呀,不多講,老規矩,先看效果再來看代碼:
一:應用剛啟動時:
<a target="_blank" href="http://blog.51cto.com/attachment/201201/155450862.png"></a>
二:檢視系統預設接收到通知時的效果圖:
<a target="_blank" href="http://blog.51cto.com/attachment/201201/155502337.png"></a>
三:自定義通知小提示效果圖:
<a target="_blank" href="http://blog.51cto.com/attachment/201201/155521729.png"></a>
四:自定義通知布局與系統預設布局對比
<a target="_blank" href="http://blog.51cto.com/attachment/201201/155531649.png"></a>
有了效果圖後再後代碼就簡單多了,直接看看代碼,小馬就直接在源代碼裡面加了注釋,有不妥之處還請朋友們提出來 :
package com.xiaoma.www.demo;
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;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* @Title: NotificationDemoActivity.java
* @Package com.xiaoma.www.demo
* @Description: 通知控制類
* @author MZH
*/
public class NotificationDemoActivity extends Activity {
private Button clearBtn ;
private NotificationManager manager ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
/**
* 初始化方法實作
*/
private void init(){
//步驟一:取得系統服務
manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//步驟二:
Notification notification = new Notification(R.drawable.notification,
"收到小馬通知測試", System.currentTimeMillis());
/**
* 小馬在這個地方寫下為什麼要在用到通知的時候要建立PendingIntent對象,是因為
* Notification可以與應用程式脫離,即便應用程式關閉,Notification仍然
* 會顯示在狀态欄中,當應用程式再次啟動後,又可以重新控制這些Nofication消息,
* 如清除或替換它們,因為才建立的此對象,更神奇的是這個對象由安卓系統本身維護哦,是以
* 在應用關閉後這個對象是不會被翻譯掉的
*/
//步驟三:
Intent intent = new Intent();
intent.putExtra("Msg", "這是從Notification傳遞過來的資訊");
intent.setClass(this, NotificationDemoTest.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);
//步驟四:setLatestEventInfo通過标準的方式将我們的通知設定到指定的View中
notification.setLatestEventInfo(this, "通知測試哦", "這是通知的主内容", contentIntent);
//寫下面這句話的時候大家注意下不要忘了加震動權限,不然沒法調用硬體
//notification.defaults = Notification.DEFAULT_VIBRATE;
//下面這句是把目前的通知設定永久儲存的Notification,好暴力呀,吼吼
//notification.flags = Notification.FLAG_NO_CLEAR
//下面這句是指:如果要讓其它的軟體檢測到永久儲存的通知時可以這樣寫
//Notification.flags = Nofication.FLAG_ONGOING_EVENT;
* 在這一步需要指定辨別Notification的唯一ID,這個ID必須相對于同一個
* NoficationManager對象是唯一的,否則就會覆寫相同ID的Notification
//步驟五:
manager.notify(R.drawable.notification, notification);
clearBtn = (Button)findViewById(R.id.button1);
clearBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
manager.cancel(R.drawable.notification);
//清除所有通知:
//manager.cancelAll();
}
});
}
再來看下接收上面這個通知通過Intent發給第二個Activity的代碼,雖然很簡單,但有這個後,大家就可以随便更改系統級的通知咯,不用老是用系統很死闆很統一的布局,寫出自己的個性啦,吼吼,加油加油,看代碼了:
import android.widget.RemoteViews;
import android.widget.Toast;
* @Title: NotificationDemoTest.java
* @Package com.xiaoma.www.demo
* @Description: 接收并彈出通知傳遞過來的資訊
* @author MZH
*/
public class NotificationDemoTest extends Activity {
//聲明變量
private Button selfBtn;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setContentView(R.layout.maintwo);
Intent i = this.getIntent();
String msg = i.getStringExtra("Msg");
if(!"".equals(msg)){
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "未接收到任何短信", Toast.LENGTH_SHORT).show();
}
selfBtn = (Button)findViewById(R.id.button2);
selfBtn.setOnClickListener(new OnClickListener() {
sendNotification();
private void sendNotification(){
* 下面還是五個步驟,呵呵,跟前面那個Activity是一樣的,隻是通知布局不同咯,用RemoteViews加載
Notification notification = new Notification(R.drawable.ic_launcher,
"這是自定義通知的資訊哦", System.currentTimeMillis());
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, getIntent(), 1);
* RemoteViews這個類的官方文檔解釋為:
* 很直接很簡單的講就是:從我們自己的XML檔案中加載一個VIEW到通知中
RemoteViews rViews = new RemoteViews(getPackageName(), R.layout.notification);
rViews.setTextViewText(R.id.textView, "更新自定義内容");
notification.contentView = rViews;
notification.contentIntent = pendingIntent;
//notification.setLatestEventInfo(this, "這是通知的标題", "這是通知的正文", pendingIntent);
manager.notify(1, notification);
這個代碼雖然很簡單,但小馬想通過自己的積累,哪怕每天積累一點點知識點,足矣,吼吼,記錄下自己成長的過程,如果朋友們對我的小DEMO有什麼好的建議請直接跟小馬講,一定向各位虛心學習,先謝謝啦,最後 ,老樣子,這個小DEMO的源碼小馬還是會放到附件中去,希望朋友們發現問題請直接指出,謝謝
<a href="http://down.51cto.com/data/2359705" target="_blank">附件:http://down.51cto.com/data/2359705</a>
本文轉自華華世界 51CTO部落格,原文連結:http://blog.51cto.com/mzh3344258/767219,如需轉載請自行聯系原作者