给地震监视器添加Notification
在接下来的例子中,EarthquakeService将为每个新的地震触发一个Notification。显示状态条图标的同时,在扩展的状态窗口中显示地震的级别和位置,选择它将会打开Earthquake Activity。
1. 在EarthquakeService中,创建一个新的Notification实例变量来储存Notification对象,用于控制状态条图标和扩展的状态窗口中项目的细节。
private Notification newEarthquakeNotification;
public static final int NOTIFICATION_ID = 1;
2. 扩展onCreate方法来创建Notification对象。
@Override
public void onCreate() {
updateTimer = new Timer(“earthquakeUpdates”);
int icon = R.drawable.icon;
String tickerText = “New Earthquake Detected”;
long when = System.currentTimeMillis();
newEarthquakeNotification= new Notification(icon, tickerText, when);
}
3. 现在,扩展annouceNewQuake方法,在每个新的地震数据添加到ContentProvider之后触发Notification。在初始化Notification之前,使用setLastestEventInfo方法来更新扩展的信息。
private void announceNewQuake(Quake quake) {
String svcName = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager;
notificationManager = (NotificationManager)getSystemService(svcName);
Context context = getApplicationContext();
String expandedText = quake.getDate().toString();
String expandedTitle = “M:” + quake.getMagnitude() + “ “ +quake.getDetails();
Intent startActivityIntent = new Intent(this, Earthquake.class);
PendingIntent launchIntent = PendingIntent.getActivity(context,0,startActivityIntent,0);
newEarthquakeNotification.setLatestEventInfo(context,expandedTitle,expandedText,launchIntent);
newEarthquakeNotification.when = java.lang.System.currentTimeMillis();
notificationManager.notify(NOTIFICATION_ID, newEarthquakeNotification);
Intent intent = new Intent(NEW_EARTHQUAKE_FOUND);
intent.putExtra(“date”, quake.getDate().getTime());
intent.putExtra(“details”, quake.getDetails());
intent.putExtra(“longitude”, quake.getLocation().getLongitude());
intent.putExtra(“latitude”, quake.getLocation().getLatitude());
intent.putExtra(“magnitude”, quake.getMagnitude());
sendBroadcast(intent);
}
4. 最后一步是在两个Activity类中清除Notification。当应用程序活跃时,通过移除状态图标来完成。
4.1. 在Earthquake Activity中,修改onCreate方法,获取NotificationManager的一个引用。
NotificationManager notificationManager;
@Override
public void onCreate(Bundle icicle) {
[ ... existing onCreate ... ]
String svcName = Context.NOTIFICATION_SERVICE;
notificationManager = (NotificationManager)getSystemService(svcName);
}
4.2. 修改EarthquakeReceiver的onReceive方法。当这个方法执行时,正好是Activity活跃的时候,你可以在这里安全的取消所有的地震Notification。
@Override
public void onReceive(Context context, Intent intent) {
loadQuakesFromProvider();
notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);
}
4.3. 接下来,扩展onResume方法来取消Notification。
@Override
public void onResume() {
notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);
IntentFilter filter;
filter = new IntentFilter(EarthquakeService.NEW_EARTHQUAKE_FOUND);
receiver = new EarthquakeReceiver();
registerReceiver(receiver, filter);
super.onResume();
}
4.4. 在EarthquakeMap Activity中重复相同的过程。
NotificationManager notificationManager;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.earthquake_map);
ContentResolver cr = getContentResolver();
earthquakeCursor = cr.query(EarthquakeProvider.CONTENT_URI, null, null, null, null);
MapView earthquakeMap = (MapView)findViewById(R.id.map_view);
earthquakeMap.getOverlays().add(new EarthquakeOverlay(earthquakeCursor));
String svcName = Context.NOTIFICATION_SERVICE;
notificationManager = (NotificationManager)getSystemService(svcName);
}
@Override
public void onResume() {
notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);
earthquakeCursor.requery();
IntentFilter filter;
filter = new IntentFilter(EarthquakeService.NEW_EARTHQUAKE_FOUND);
receiver = new EarthquakeReceiver();
registerReceiver(receiver, filter);
super.onResume();
}
public class EarthquakeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);
earthquakeCursor.requery();
MapView earthquakeMap = (MapView)findViewById(R.id.map_view);
earthquakeMap.invalidate();
}
}
转载于:https://www.cnblogs.com/xirihanlin/archive/2009/08/24/1552725.html
给地震监视器添加Notification相关推荐
- Android 给地震监视器添加Notification
2019独角兽企业重金招聘Python工程师标准>>> 在接下来的例子中,EarthquakeService将为每个新的地震触发一个Notification.显示状态条图标的同时,在 ...
- SystemUI之状态栏notification icon加载流程
2019独角兽企业重金招聘Python工程师标准>>> 引言 今天我们主要讲的是SystemUI状态栏里面另一个常见的icons--notification icons,该icons ...
- Libev源码分析02:Libev中的IO监视器
一:代码流程 在Libev中,启动一个IO监视器,等待该监视器上的事件触发,然后调用该监视器的回调函数.整个的流程是这样的: 首先调用ev_default_loop初始化struct ev_loop ...
- Lumerical FDTD Solutions中圆形监视器和分析组的设计
Lumerical FDTD Solutions中内置的监视器只有点(零维).线(一维).矩形(二维).长方体(三维)等基本形状,但在一些特殊的仿真中,实验者可能想要其他形状的监视器,例如圆形.球形. ...
- Android 4高级编程(第3版)》
<Android 4高级编程(第3版)> 基本信息 原书名:Professional Android 4 Application Development 作者: (英)Reto Meier ...
- Android 4高级编程(第3版)
<Android 4高级编程(第3版)> 基本信息 原书名:Professional Android 4 Application Development 作者: (英)Reto Meier ...
- 《Android 4高级编程(第3版)》(完整书签).pdf
下载地址:网盘下载 内容简介 编辑 <Android 4高级编程(第3版)>由Android权威专家编写,涵盖了所有最新的内容,是学习使用Android 4 SDK开发移动应用程序的理想指 ...
- 在视图控制器之间传递数据
我是iOS和Objective-C以及整个MVC范例的新手,但我坚持以下几点: 我有一个充当数据输入表单的视图,我想给用户选择多个产品的选项. 这些产品在另一个带有UITableViewControl ...
- es中的ResourceWatcherService
node启动时, 会创建ResourceWatcherService final ResourceWatcherService resourceWatcherService = new Resourc ...
最新文章
- vs2008中常见错误解决方法汇总
- R包库安装及数据加载:一次安装多个R包、一次加载多个R包
- 兰蔻御用运维总结之一
- leetcode165. 比较版本号 超级重要的细节
- Python的主要8大应用领域,你还不会吗?
- springmvc+json 前后台数据交互
- 常见面试算法:k-近邻算法原理与python案例实现
- jsoncpp解析json报文测试
- 从Python迁移到Go的原因和好处
- FAT32文件系统结构详解
- x4无法在此计算机上安装,错误写入注册表键_cdrx4安装提示不能将数值写入键_错误写入注册表键...
- ASP.NET身份验证和授权,使用cookie和Claims认证
- iPad及BT4下的WEP破解实验与分析 | Network Security
- calipso是什么意思_library是什么意思_library的翻译_音标_读音_用法_例句_爱词霸在线词典...
- Python实现按键精灵(一)录制脚本
- 刀剑无双服务器显示404,刀剑无双
- A - Fibonacci
- 特辑·写给SoftwareTeacher的一封信
- Java关键字(48个关键字、2个保留字、3个特殊直接量)
- 互联网医疗泡沫破灭后 或将迎来持久良性发展
热门文章
- 【附源码】计算机毕业设计SSM商品推荐系统
- winform中添加Windows Media Player
- mac系统计算机名,苹果电脑系统各版本名字该怎么念?
- 商城系统搭建支付通道
- 电脑提示计算机缺失程序怎么办,电脑安装扫描仪驱动程序,提示文件丢失怎么办?这样解决...
- windows系统目录programdata和program file(x86)
- 3个python库的图像增强
- Snell定律(折射定律)之导数的应用
- 联想服务器系统改成win7,联想Win8系统换成Win7的步骤
- 抖音删除作品会有哪些影响,限流降权该如何挽回丨国仁网络资讯