android pendingintent 广播,android-requestLocationUpdates与PendingIntent和广播...

我已经设置了一个由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);

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值