Notification

本文详细介绍了如何在安卓系统中使用NotificationManager实现状态栏的通知功能。包括创建Notification、设置Notification的样式、使用PendingIntent实现点击后的操作等。此外还讲解了如何通过NotificationManager的notify方法显示通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Notification状态栏图标与文字提醒,只要将Notification添加到NotificationManager,即可将信息显示到状态栏上。
获取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);

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值