给地震监视器添加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. 修改EarthquakeReceiveronReceive方法。当这个方法执行时,正好是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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值