Android Services學習--Notification
package com.lxt008;
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.widget.Button;
public class Activity01 extends Activity
{
Button m_Button1, m_Button2, m_Button3, m_Button4;
//聲明通知(消息)管理器
NotificationManager m_NotificationManager;
Intent m_Intent;
PendingIntent m_PendingIntent;
//聲明Notification對象
Notification m_Notification;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化NotificationManager對象
m_NotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//獲取4個按鈕對象
m_Button1 = (Button) findViewById(R.id.Button01);
m_Button2 = (Button) findViewById(R.id.Button02);
m_Button3 = (Button) findViewById(R.id.Button03);
m_Button4 = (Button) findViewById(R.id.Button04);
//點擊通知時轉移內容
m_Intent = new Intent(Activity01.this, Activity02.class);
//主要是設置點擊通知時顯示內容的類
m_PendingIntent = PendingIntent.getActivity(Activity01.this, 0, m_Intent, 0);
//構造Notification對象
m_Notification = new Notification();
m_Button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
//設置通知在狀態欄顯示的圖標
m_Notification.icon = R.drawable.img1;
//當我們點擊通知時顯示的內容
m_Notification.tickerText = "Button1通知內容...........";
//通知時發出默認的聲音
m_Notification.defaults = Notification.DEFAULT_SOUND;
//設置通知顯示的參數
m_Notification.setLatestEventInfo(Activity01.this, "Button1", "Button1通知", m_PendingIntent);
//可以理解爲執行這個通知
m_NotificationManager.notify(0, m_Notification);
}
});
m_Button2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
m_Notification.icon = R.drawable.img2;
m_Notification.tickerText = "Button2通知內容...........";
//通知時震動
m_Notification.defaults = Notification.DEFAULT_VIBRATE;
m_Notification.setLatestEventInfo(Activity01.this, "Button2", "Button2通知", m_PendingIntent);
m_NotificationManager.notify(0, m_Notification);
}
});
m_Button3.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
m_Notification.icon = R.drawable.img3;
m_Notification.tickerText = "Button3通知內容...........";
//通知時屏幕發亮
m_Notification.defaults = Notification.DEFAULT_LIGHTS;
m_Notification.setLatestEventInfo(Activity01.this, "Button3", "Button3通知", m_PendingIntent);
m_NotificationManager.notify(0, m_Notification);
}
});
m_Button4.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
m_Notification.icon = R.drawable.img4;
m_Notification.tickerText = "Button4通知內容..........";
//通知時既震動又屏幕發亮還有默認的聲音
m_Notification.defaults = Notification.DEFAULT_ALL;
m_Notification.setLatestEventInfo(Activity01.this, "Button4", "Button4通知", m_PendingIntent);
m_NotificationManager.notify(0, m_Notification);
}
});
}
}
一、第一種比較實用
//點擊通知時轉移內容
m_Intent = new Intent(Activity01.this, Activity02.class);
//主要是設置點擊通知時顯示內容的類
m_PendingIntent = PendingIntent.getActivity(Activity01.this, 0, m_Intent, 0);
//設置通知在狀態欄顯示的圖標
m_Notification.icon = R.drawable.img1;
//當我們點擊通知時顯示的內容
m_Notification.tickerText = "Button1通知內容...........";
//通知時發出默認的聲音
m_Notification.defaults = Notification.DEFAULT_SOUND;
//設置通知顯示的參數
m_Notification.setLatestEventInfo(Activity01.this, "Button1", "Button1通知", m_PendingIntent);
//可以理解爲執行這個通知
m_NotificationManager.notify(0, m_Notification);
通知欄伴有音效提示,顯示圖標,Button1通知內容...................,下拉通知欄時,顯示APP的ICON及標題Button1,內容 Button1通知。點擊通知內容時跳轉到另一個Activity
Pendingintent,一般用在 Notification上,可以理解爲延遲執行的intent,PendingIntent是對Intent一個包裝。