Android 给地震监视器添加Notification

原创
2013/07/16 16:33
阅读数 75
在接下来的例子中,EarthquakeService将为每个新的地震触发一个Notification。显示状态条图标的同时,在扩展的状态窗口中显示地震的级别和位置,选择它将会打开Earthquake Activity。

1. 在EarthquakeService中,创建一个新的Notification实例变量来储存Notification对象,用于控制状态条图标和扩展的状态窗口中项目的细节。

Java代码:
privateNotificationnewEarthquakeNotification;
publicstaticfinalintNOTIFICATION_ID=1;

2. 扩展onCreate方法来创建Notification对象。
java代码:
@Override
publicvoidonCreate(){
updateTimer=newTimer(“earthquakeUpdates”);
inticon=R.drawable.icon;
StringtickerText=“NewEarthquakeDetected”;
longwhen=System.currentTimeMillis();
newEarthquakeNotification=newNotification(icon,tickerText,when);
}

3. 现在,扩展annouceNewQuake方法,在每个新的地震数据添加到ContentProvider之后触发Notification。在初始化Notification之前,使用setLastestEventInfo方法来更新扩展的信息。
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);
}

4. 最后一步是在两个Activity类中清除Notification。当应用程序活跃时,通过移除状态图标来完成。4.1. 在Earthquake Activity中,修改onCreate方法,获取NotificationManager的一个引用。
java代码:
NotificationManagernotificationManager;
@Override
publicvoidonCreate(Bundleicicle){
[...existingonCreate...]
StringsvcName=Context.NOTIFICATION_SERVICE;
notificationManager=(NotificationManager)getSystemService(svcName);
}

4.2. 修改EarthquakeReceiver的onReceive方法。当这个方法执行时,正好是Activity活跃的时候,你可以在这里安全的取消所有的地震Notification。
java代码:
@Override
publicvoidonReceive(Contextcontext,Intentintent){
loadQuakesFromProvider();
notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);
}

4.3. 接下来,扩展onResume方法来取消Notification。
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代码:
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();
}
}


展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部