下面來談?wù)刵otification讲仰,這個notification一般用在電話,短信漾峡,郵件攻旦,鬧鐘鈴聲,在手機的狀態(tài)欄上就會出現(xiàn)一個小圖標(biāo)生逸,提示用戶處理這個快訊牢屋,這時手從上方滑動狀態(tài)欄就可以展開并處理這個快訊。發(fā)現(xiàn)這個功能特別好用槽袄,所以我就根據(jù)我的理解來談?wù)劺游蕖U詭椭臋n : notification類表示一個持久的通知,將提交給用戶使用NotificationManager遍尺。已添加的Notification.Builder截酷,使其更容易構(gòu)建通知。notification是一種讓你的應(yīng)用程序在沒有開啟情況下或在后臺運行警示用戶乾戏。它是看不見的程序組件(Broadcast Receiver迂苛,Service和不活躍的Activity)警示用戶有需要注意的事件發(fā)生的最好途徑。
先來區(qū)分以下**狀態(tài)欄和狀態(tài)條**的區(qū)別:
1鼓择、狀態(tài)條就是手機屏幕最上方的一個條形狀的區(qū)域三幻;
在狀態(tài)條有好多信息量:比如usb連接圖標(biāo),手機信號圖標(biāo)呐能,電池電量圖標(biāo)念搬,時間圖標(biāo)等等;
2摆出、狀態(tài)欄就是手從狀態(tài)條滑下來的可以伸縮的view朗徊;
在狀態(tài)欄中一般有兩類(使用FLAG_標(biāo)記):
(1)正在進行的程序;
(2)是通知事件偎漫;
大概來描述創(chuàng)建一個Notification傳送的信息有:
1荣倾、**一個狀態(tài)條圖標(biāo);**
2骑丸、**在拉伸的狀態(tài)欄窗口中顯示帶有大標(biāo)題舌仍,小標(biāo)題妒貌,圖標(biāo)的信息,并且有處理該點擊事件:比如調(diào)用該程序的入口類铸豁; **
** 3灌曙、閃光,LED节芥,或者震動在刺;**
快速創(chuàng)建一個Notification的步驟簡單可以分為以下四步:
**第一步**:通過getSystemService()方法得到NotificationManager對象;
**第二步**:對Notification的一些屬性進行設(shè)置比如:內(nèi)容头镊,圖標(biāo)蚣驼,標(biāo)題,相應(yīng)notification的動作進行處理等等相艇;
**第三步**:通過NotificationManager對象的notify()方法來執(zhí)行一個notification的快訊颖杏;
** 第四步**:通過NotificationManager對象的cancel()方法來取消一個notificatioin的快訊;
下面對Notification類中的一些常量坛芽,字段留储,方法簡單介紹一下:
常量:
DEFAULT_ALL 使用所有默認值,比如聲音咙轩,震動获讳,閃屏等等
DEFAULT_LIGHTS 使用默認閃光提示
DEFAULT_SOUNDS 使用默認提示聲音
DEFAULT_VIBRATE 使用默認手機震動
【**說明】:加入手機震動,一定要在manifest.xml中加入權(quán)限:**
** ****<uses-permission android:name="android.permission.VIBRATE" />**
以上的效果常量可以疊加,即通過
mNotifaction.defaults =DEFAULT_SOUND | DEFAULT_VIBRATE ;
或mNotifaction.defaults |=DEFAULT_SOUND (最好在真機上測試活喊,震動效果模擬器上沒有)
//設(shè)置flag位
** FLAG_AUTO_CANCEL** 該通知能被狀態(tài)欄的清除按鈕給清除掉
** FLAG_NO_CLEAR** 該通知能被狀態(tài)欄的清除按鈕給清除掉
FLAG_ONGOING_EVENT 通知放置在正在運行
** FLAG_INSISTENT ** 是否一直進行丐膝,比如音樂一直播放,知道用戶響應(yīng)
常用字段:
**contentIntent** 設(shè)置PendingIntent對象钾菊,點擊時發(fā)送該Intent
defaults 添加默認效果
flags 設(shè)置flag位帅矗,例如FLAG_NO_CLEAR等
**icon ** 設(shè)置圖標(biāo)
sound 設(shè)置聲音
**tickerText** 顯示在狀態(tài)欄中的文字
** when **發(fā)送此通知的時間戳
Notification.build構(gòu)造Notification方法介紹: ** **
** ** void setLatestEventInfo(Context context , CharSequencecontentTitle,CharSequence contentText,PendingIntent contentIntent)
** **功能: 顯示在拉伸狀態(tài)欄中的Notification屬性,點擊后將發(fā)送PendingIntent對象
參數(shù): context 上下文環(huán)境
contentTitle 狀態(tài)欄中的大標(biāo)題
contentText 狀態(tài)欄中的小標(biāo)題
contentIntent 點擊后將發(fā)送PendingIntent對象
說明:要是在Notification中加入圖標(biāo)结缚,在狀態(tài)欄和狀態(tài)條中顯示圖標(biāo)一定要用這個方法,否則報錯软棺!
最后說一下NotificationManager類的常用方法:
通過獲取系統(tǒng)服務(wù)來獲取該對象:
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE) ;
** NotificationManager常用方法介紹:**
public void cancelAll() 移除所有通知 (只是針對當(dāng)前Context下的Notification)
public void cancel(int id) 移除標(biāo)記為id的通知 (只是針對當(dāng)前Context下的所有Notification)
public void** **notify(String tag ,int id, Notification notification) 將通知加入狀態(tài)欄, 標(biāo)簽為tag红竭,標(biāo)記為id
public void notify(int id, Notification notification) 將通知加入狀態(tài)欄,,標(biāo)記為id
下面看一下demo的效果圖:
源碼奉上:
在NotificationApp工程里面:
一喘落、在com.cn.notification.daming包下面NotificationMainActivity.java中的代碼:
package com.cn.notification.daming;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class NotificationMainActivity extends Activity implements OnClickListener {
private Button setNotificationSoundBtn = null;
private Button showNotificatioBtn = null;
private Button cancelNotificationBtn = null;
private Intent mIntent = null;
private PendingIntent mPendingIntent = null;
private Notification mNotification = null;
private NotificationManager mNotificationManager = null;
private static final int NOTIFICATION_STATE = 1;
private static final int RINGTONE_PICKED = 2;
private Uri notifiSounds = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
setNotificationSoundBtn = (Button)findViewById(R.id.button0);
setNotificationSoundBtn.setOnClickListener(this);
showNotificatioBtn = (Button)findViewById(R.id.button1);
showNotificatioBtn.setOnClickListener(this);
cancelNotificationBtn = (Button)findViewById(R.id.button2);
cancelNotificationBtn.setOnClickListener(this);
mIntent = new Intent(this, ToNotificationActivity.class);
mPendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
mNotification = new Notification();
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button0:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
// Allow user to pick 'Default'
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
// Show only ringtones
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
// Don't show 'Silent'
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
String notifi_sound = sharedPreferences.getString("notification_sounds", null);
if(notifi_sound != null){
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(notifi_sound));
}
// Launch!
startActivityForResult(intent, RINGTONE_PICKED);
break;
case R.id.button1:
mNotification.icon = R.drawable.daming;
mNotification.tickerText = "大明ZeroSon Notification";
mNotification.sound = notifiSounds;
mNotification.defaults = Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS;
mNotification.flags = Notification.FLAG_INSISTENT ;
mNotification.setLatestEventInfo(this, "大明Notification", "This is Daming`s Notification Test!", mPendingIntent);
mNotificationManager.notify(NOTIFICATION_STATE, mNotification);
break;
case R.id.button2:
mNotificationManager.cancel(NOTIFICATION_STATE);
break;
default:break;
}
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
if (resultCode != RESULT_OK) {
return;
}
switch (requestCode) {
case RINGTONE_PICKED: {
notifiSounds = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
editor.putString("notification_sounds", notifiSounds.toString());
editor.commit();
break;
}
default: break;
}
}
}
二茵宪、在com.cn.notification.daming包下面ToNotificationActivity.java中的代碼:
package com.cn.notification.daming;
import android.app.Activity;
import android.content.SharedPreferences;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;
public class ToNotificationActivity extends Activity{
private TextView textview = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
textview = (TextView)findViewById(R.id.textview);
String notificationsound = sharedPreferences.getString("notification_sounds", null);
if(notificationsound == null){
textview.setText("默認Notification聲音");
} else{
Ringtone ringtone = RingtoneManager.getRingtone(ToNotificationActivity.this, Uri.parse(notificationsound));
String title = ringtone.getTitle(ToNotificationActivity.this);
textview.setText(title);
}
}
}
三、在layout下main.xml布局文件的代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:gravity="center"
android:layout_marginBottom="10dip"
/>
<Button
android:id="@+id/button0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="設(shè)置Notification的sounds"
android:layout_marginBottom="10dip"
/>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="發(fā)送Notification"
android:layout_marginBottom="10dip"
/>
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="取消Notification"
android:layout_marginBottom="10dip"
/>
</LinearLayout>
四瘦棋、在layout下main1.xml布局文件的代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="10pt"
android:text="大明原創(chuàng)"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
/>
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="10pt"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
/>
</LinearLayout>
五稀火、manifest.xml中的代碼:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cn.notification.daming"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.VIBRATE" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".NotificationMainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ToNotificationActivity"></activity>
</application>
</manifest>