在本范例中,要如何把提示信息放入状态栏,又要如何显示小图标呢??Android API为了管理提示信息,定义了一个NotificationManager(Android.app.NotificationManager),只要 将Notification添加到NotificationManager,即可将信息显示在状态栏中。
本范例将模拟添加MSN在线状态的切换,在切换在线状态的同时,改变状态栏上显示的在线状态小图标,并以文字提示目前的状态为何。
test.java
package com.allove;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class Test extends Activity {
private Spinner mSpinner;
private NotificationManager mNotificationManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mNotificationManager = (NotificationManager) this
.getSystemService(Service.NOTIFICATION_SERVICE);
mSpinner = (Spinner) this.findViewById(R.id.Spinner01);
mSpinner.setAdapter(new ArrayAdapter<String>(Test.this,
android.R.layout.simple_spinner_item, getResources()
.getStringArray(R.array.spiner_string)));
mSpinner
.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
switch (arg2) {
case 0:
setNotiType(R.drawable.msn, "在线");
break;
case 1:
setNotiType(R.drawable.away, "离开");
break;
case 2:
setNotiType(R.drawable.busy, "忙碌");
break;
case 3:
setNotiType(R.drawable.min, "马上回来");
break;
case 4:
setNotiType(R.drawable.offine, "离线");
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
protected void setNotiType(int iconId, String s) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(this, Test2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent appIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification myNoti = new Notification();
myNoti.icon = iconId;
myNoti.tickerText = s;
myNoti.defaults = Notification.DEFAULT_SOUND;
myNoti.setLatestEventInfo(this, "MSN登陆状态", s, appIntent);
mNotificationManager.notify(0, myNoti);
}
}
发表评论 评论 (0 个评论)