2010年12月26日星期日

狀態欄通知(status bar notification)

狀態欄通知(status bar notification)在系統的狀態欄之上添加一個圖標(連帶有可選的文本信息), 以及在“通知“窗口之上添加一個擴展信息.

狀態欄通知(status bar notification)在系統的狀態欄之上添加一個帶有文本信息的圖標
通知窗口之上添加的擴展信息

創建狀態欄通知的基本步驟:

1 - 獲取NotificationManager引用對象(Get a reference to the NotificationManager)

2 - 實例化(Instantiate the Notification)

3 - 定義通知擴展的消息和意圖(Define the Notification's expanded message and Intent)

4 - 傳遞通知給其NotificationManager(Pass the Notification to the NotificationManager)

下面是一個簡單的例子:

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"
/>
<Button
android:id="@+id/fire"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Fire Notification -"
/>
</LinearLayout>


AndroidStatusBarNotifications.java
package com.AndroidStatusBarNotifications;

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.widget.Button;

public class AndroidStatusBarNotifications extends Activity {

private static final int ID_My_Notification = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonFire = (Button)findViewById(R.id.fire);
buttonFire.setOnClickListener(buttonFireOnClickListener);
}

private Button.OnClickListener buttonFireOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

//Get a reference to the NotificationManager
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

//Instantiate the Notification
int icon = android.R.drawable.ic_dialog_alert;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, when);

//Define the Notification's expanded message and Intent
Context context = getApplicationContext();
CharSequence contentTitle = "My Notification";
CharSequence contentText = "Hello My Notification!";
//Intent notificationIntent = new Intent(AndroidStatusBarNotifications.this, AndroidStatusBarNotifications.class);
Intent notificationIntent = new Intent(getBaseContext(), AndroidStatusBarNotifications.class);
PendingIntent contentIntent = PendingIntent.getActivity(AndroidStatusBarNotifications.this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

//Pass the Notification to the NotificationManager
mNotificationManager.notify(ID_My_Notification, notification);

}};
}



相關文章:
- 如何取消狀態欄通知(status bar notification)



沒有留言:

發佈留言