狀態欄提醒Notification,NotificationManager

今天學習android一下,分享一下代碼:
一個Activity或者Service可以初始化一個狀態通知。然而,因爲一個activity只有運行在前臺並且它所在的窗口獲得焦點的時候,它才能執行動作,所以


你通常需要用service來創建通知。當用戶正在使用其他應用或者設備待機時,通知可以由後臺創建。爲了創建通知,你必須用到兩個類:Notification和


NotificationManager。
使用Notification類的一個實例去定義狀態通知的屬性,如圖標,通知信息和一些其他的設定,如播放的聲音。NotificationManager是系統服務,它來執


行和管理所有的狀態通知。你不需要直接初始化NotificationManager。爲了把你的通知給它,你必須使用getSystemService()檢索到指向


NotificationManager的引用,然後,當你要通知用戶的時候,使用android.app.Notification) notify()把你的Notification傳給它。
按如下方式創建狀態通知:
1. 得到指向NotificationManager的引用:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
2. 初始化Notification:
int icon = R.drawable.notification_icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
3. 定義通知的信息和PendingIntent:
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
4. 把Notification傳遞給NotificationManager:
private static final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
上面是簡單說一下基礎。詳細可以查看API:http://developer.android.com/guide/topics/ui/notifiers/notifications.html       
代碼:JAVA:
package com.example.test;
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;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Context mcontext;
private Button showButton,cancelButton;
private Notification mnotification;
private NotificationManager mnotificationmanager;
private final static int NOTIFICATION_ID = 0x0001;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==showButton){
Intent intent = new Intent(mcontext,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendIntent = PendingIntent.getActivity(mcontext, 0, intent, 0);
 //這裏必需要用setLatestEventInfo(上下文,標題,內容,PendingIntent)不然會報錯.
mnotification.setLatestEventInfo(mcontext, "10086", "您的當前話費不足,請充值.哈哈~常聯繫哦!", pendIntent);
//這裏發送通知(消息ID,通知對象)
mnotificationmanager.notify(NOTIFICATION_ID, mnotification);
}else{
//取消信息欄提示(根據ID)
mnotificationmanager.cancel(NOTIFICATION_ID);
}
}
//初始化對象
@SuppressWarnings("deprecation")
private void init(){
mcontext = MainActivity.this;
showButton =(Button)findViewById(R.id.showbn);
cancelButton =(Button)findViewById(R.id.cancelbn);
mnotification = new Notification(R.drawable.ic_launcher,"This is a notification",System.currentTimeMillis());
mnotificationmanager = (NotificationManager) getSystemService(mcontext.NOTIFICATION_SERVICE);
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
        />
    <Button 
        android:id="@+id/showbn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="showNotification"
        />
    
    <Button 
        android:id="@+id/cancelbn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cancelNotification"
        />
</LinearLayout>

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