Android 给地震监视器添加Notification

在接下来的例子中,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();
}
}


转载于:https://my.oschina.net/microoh/blog/144958

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值