我已经设置了一个由BroadcastReceiver接收到的警报,该警报将启动WakefulIntentService(LocationMonitor类).在LocationMonitor中,我有:
private static final int MIN_TIME_BETWEEN_SCANS = 1 * 30 * 1000;
private static final int MIN_DISTANCE = 0;
@Override
protected void doWakefulWork(Intent intent) {
final CharSequence action = intent.getAction();
if (action == null) { // monitor command from the alarm manager
// the call below enables the LocationReceiver
BaseReceiver.enable(this, ENABLE, LocationReceiver.class);
if (lm == null) lm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
Intent i = new Intent(this, LocationReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, NOT_USED, i,
PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME_BETWEEN_SCANS, MIN_DISTANCE, pi);
} else if (ac_location_data.equals(action)) {
final Bundle extras = intent.getExtras();
if (extras != null) {
final Location loc = (Location) extras
.get(LocationManager.KEY_LOCATION_CHANGED);
if (loc == null) {
w("NULL LOCATION - EXTRAS : " + extras); //Log.w
// while gps is disabled I keep getting this :
// NULL LOCATION - EXTRAS : Bundle[{providerEnabled=false}]
} else {
final double lon = loc.getLongitude();
final double lat = loc.getLatitude();
w("latitude :" + lat + " -- longitude : " + lon);
}
}
}
}
我上面的代码有几个问题.
>如果最初禁用了GPS,然后启用了它,则会得到一堆W / GpsLocationProvider(…):不需要删除uid 1000的侦听器.警告来自here.在代码中找不到该删除位置触发的侦听器数量,也看不到将uid 1000(系统服务器apparently)分配给的位置.
>当我启用GPS时,我会按预期获得位置,然后出现“ RemoteException”
LocationManagerService(…): RemoteException calling onLocationChanged on Receiver{4083ee68 Intent PendingIntent{4084e6b8: PendingIntentRecord{4083ef78 gr.uoa.di.monitoring.android broadcastIntent}}}mUpdateRecords: {gps=UpdateRecord{40838180 mProvider: gps mUid: 10064}}
这实际上不是RemoteException,而只是PendingIntent.CancelledException-消息很容易引起误解.大概我想:它来自here,调用this.我的问题是:为什么它会重复使用Intent-FLAG_ONE_SHOT是否不应该处置它?
但是最重??要的问题是:当我这样注册一个PendingIntent时,我希望收到什么意图?我应该使用哪些标志?
请记住,我使用的是这种模式,因为即使在睡眠状态下我也希望手机更新其位置(这确实可以实现位置更新).我尝试使用FLAG_ONE_SHOT模拟requestSingleUpdate(在2.3中不可用).
接收方:
public final class LocationReceiver extends BaseReceiver {
private static final Class extends Monitor> MONITOR_CLASS =
LocationMonitor.class;
@Override
public void onReceive(Context context, Intent intent) {
d(intent.toString());
final String action = intent.getAction();
d(action + "");
final Intent i = new Intent(context, MONITOR_CLASS);
i.fillIn(intent, 0); // TODO do I need flags ?
i.setAction(ac_location_data.toString());
WakefulIntentService.sendWakefulWork(context, i);
}
}