Notification状态栏图标与文字提醒,只要将Notification添加到NotificationManager,即可将信息显示到状态栏上。
获取NotificationManager对象:
当点击状态栏上的条目时,你需要进行页面的跳转等等,这里解释一下Notification的相关参数。
PendingIntent是一个悬挂起的Intent,也就是它先不执行,在它里面真正包含了一个点击后执行的Intent,当点击条目以后,就真正执行包含在PendingIntent里面的Intent对象。
通过setLatestEventInfo()这个方法来设定Notification列表中所显示的信息标题与信息内容。
Notification的defaults属性列表:
Notification.DEFAULT_SOUND 发出系统默认的声音
Notification.DEFAULT_LIGHTS 屏幕发亮
Notification.DEFAULT_VIBRATE 手机震动
Notification.DEFAULT_ALL 包含以上是那种动作
如果你想播放指定声音,你可以设置Notification的sound属性(Url对象)来指定要播放的声音。
最后通过NotificationManager的notify方法来发出信息,其中第一个参数id就是Notification的id。如果id不同,那么状态栏上会出现Notification列表。
有关的参数可见代码:
获取NotificationManager对象:
notiManager = (NotificationManager)getApplication().getSystemService(Service.NOTIFICATION_SERVICE);
当点击状态栏上的条目时,你需要进行页面的跳转等等,这里解释一下Notification的相关参数。
PendingIntent是一个悬挂起的Intent,也就是它先不执行,在它里面真正包含了一个点击后执行的Intent,当点击条目以后,就真正执行包含在PendingIntent里面的Intent对象。
通过setLatestEventInfo()这个方法来设定Notification列表中所显示的信息标题与信息内容。
Notification的defaults属性列表:
Notification.DEFAULT_SOUND 发出系统默认的声音
Notification.DEFAULT_LIGHTS 屏幕发亮
Notification.DEFAULT_VIBRATE 手机震动
Notification.DEFAULT_ALL 包含以上是那种动作
如果你想播放指定声音,你可以设置Notification的sound属性(Url对象)来指定要播放的声音。
最后通过NotificationManager的notify方法来发出信息,其中第一个参数id就是Notification的id。如果id不同,那么状态栏上会出现Notification列表。
有关的参数可见代码:
package com.kevin.notification;
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.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class Main extends Activity {
private NotificationManager notiManager;
private Spinner spinner;
private ArrayAdapter<String> adapter;
private String[] behaviors = {"起立","敬礼"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 创建NotificationManager对象
notiManager = (NotificationManager)getApplication().getSystemService(Service.NOTIFICATION_SERVICE);
spinner = (Spinner)findViewById(R.id.spinner1);
adapter = new ArrayAdapter<String>(Main.this, android.R.layout.simple_spinner_item, behaviors);
// 设置adapter下拉菜单的布局
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// 将adapter添加到spinner中
spinner.setAdapter(adapter);
// 设置spinner的OnItemSelectedListener(还记得吗?spinner不支持OnItemClickListener事件)
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 0:
setNotiType(R.drawable.android_normal, "起立");
break;
case 1:
setNotiType(R.drawable.android_waving, "敬礼");
break;
default:
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
// 发出Notification
private void setNotiType(int iconid, String text){
// 创建新的Intent
Intent notiIntent = new Intent(Main.this,JumpActivity.class);
notiIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 创建PendingIntent最为设置递延运行的Activity
PendingIntent jumpIntent = PendingIntent.getActivity(Main.this, 0, notiIntent, 0);
// 创建notification
Notification noti = new Notification();
// 设置statusbar显示的icon
noti.icon = iconid;
// 设置statusbar显示的文字信息
noti.tickerText = text;
// 设置notification发出的声音
noti.defaults = Notification.DEFAULT_SOUND;
// 设置Notification留言的参数
noti.setLatestEventInfo(Main.this, "我的行为", text, jumpIntent);
// 发送Notification
notiManager.notify(0, noti);
}
}