以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Android 高级开发 』   (http://bbs.xml.org.cn/list.asp?boardid=53)
----  【代码】Android 添加Notification给地震监视器  (http://bbs.xml.org.cn/dispbbs.asp?boardid=53&rootid=&id=126792)


--  作者:挥戈回日
--  发布时间:7/24/2013 4:12:00 PM

--  【代码】Android 添加Notification给地震监视器
在接下来的例子中,EarthquakeService将为每个新的地震触发一个Notification。显示状态条图标的同时,在扩展的状态窗口中显示地震的级别和位置,选择它将会打开Earthquake Activity。1. 在EarthquakeService中,创建一个新的Notification实例变量来储存Notification对象,用于控制状态条图标和扩展的状态窗口中项目的细节。Java代码:
Java代码
privateNotificationnewEarthquakeNotification;
publicstaticfinalintNOTIFICATION_ID=1;
扩展onCreate方法来创建Notification对象。java代码:
Java代码
@Override
publicvoidonCreate(){
updateTimer=newTimer(“earthquakeUpdates”);
inticon=R.drawable.icon;
StringtickerText=“NewEarthquakeDetected”;
longwhen=System.currentTimeMillis();
newEarthquakeNotification=newNotification(icon,tickerText,when);
}
现在,扩展annouceNewQuake方法,在每个新的地震数据添加到ContentProvider之后触发Notification。在初始化Notification之前,使用setLastestEventInfo方法来更新扩展的信息。java代码:
Java代码
privatevoidannounceNewQuake(Quakequake){
StringsvcName=Context.NOTIFICATION_SERVICE;
NotificationManagernotificationManager;
notificationManager=(NotificationManager)getSystemService(svcName);
Contextcontext=getApplicationContext();
StringexpandedText=quake.getDate().toString();
StringexpandedTitle=“M:”+quake.getMagnitude()+““+quake.getDetails();
IntentstartActivityIntent=newIntent(this,Earthquake.class);
PendingIntentlaunchIntent=PendingIntent.getActivity(context,0,startActivityIntent,0);
newEarthquakeNotification.setLatestEventInfo(context,expandedTitle,expandedText,launchIntent);
newEarthquakeNotification.when=java.lang.System.currentTimeMillis();
notificationManager.notify(NOTIFICATION_ID,newEarthquakeNotification);
Intentintent=newIntent(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);
}
最后一步是在两个Activity类中清除Notification。当应用程序活跃时,通过移除状态图标来完成。4.1. 在Earthquake Activity中,修改onCreate方法,获取NotificationManager的一个引用。java代码: Java代码 NotificationManagernotificationManager; @Override publicvoidonCreate(Bundleicicle){ [...existingonCreate...] StringsvcName=Context.NOTIFICATION_SERVICE; notificationManager=(NotificationManager)getSystemService(svcName); }
4.2. 修改EarthquakeReceiver的onReceive方法。当这个方法执行时,正好是Activity活跃的时候,你可以在这里安全的取消所有的地震Notification。java代码:
Java代码
@Override
publicvoidonReceive(Contextcontext,Intentintent){
loadQuakesFromProvider();
notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);
}
4.3. 接下来,扩展onResume方法来取消Notification。java代码:
Java代码
@Override
publicvoidonResume(){
notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);
IntentFilterfilter;
filter=newIntentFilter(EarthquakeService.NEW_EARTHQUAKE_FOUND);
receiver=newEarthquakeReceiver();
registerReceiver(receiver,filter);
super.onResume();
}
4.4. 在EarthquakeMap Activity中重复相同的过程。java代码:
Java代码
NotificationManagernotificationManager;
@Override
publicvoidonCreate(Bundleicicle){
super.onCreate(icicle);
setContentView(R.layout.earthquake_map);
ContentResolvercr=getContentResolver();
earthquakeCursor=cr.query(EarthquakeProvider.CONTENT_URI,null,null,null,null);
MapViewearthquakeMap=(MapView)findViewById(R.id.map_view);
earthquakeMap.getOverlays().add(newEarthquakeOverlay(earthquakeCursor));
StringsvcName=Context.NOTIFICATION_SERVICE;
notificationManager=(NotificationManager)getSystemService(svcName);
}
@Override
publicvoidonResume(){
notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);
earthquakeCursor.requery();
IntentFilterfilter;
filter=newIntentFilter(EarthquakeService.NEW_EARTHQUAKE_FOUND);
receiver=newEarthquakeReceiver();
registerReceiver(receiver,filter);
super.onResume();
}
publicclassEarthquakeReceiverextendsBroadcastReceiver{
@Override
publicvoidonReceive(Contextcontext,Intentintent){
notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);
earthquakeCursor.requery();
MapViewearthquakeMap=(MapView)findViewById(R.id.map_view);
earthquakeMap.invalidate();
}
}
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
46.875ms