- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我设置了一个警报,该警报由启动 WakefulIntentService(类 LocationMonitor
)的 BroadcastReceiver 接收。在 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);
}
}
}
}
上面的代码我有几个问题。
W/GpsLocationProvider(...): Unneeded remove listener for 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 时,我希望收到什么 Intent ? 我应该使用什么标志?
请记住,我正在使用此模式,因为我想让手机更新其位置即使在睡着的时候,这实现了它(我确实获得了位置更新)。我尝试使用 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);
}
}
最佳答案
对于这个问题:
when I register a PendingIntent like this what intents do I expect to receive ? And what flags should I use ?
当您注册位置更新并传递一个PendingIntent
时,这个PendingIntent
将在LocationManager
决定通知您一个位置时被触发更新。您几乎可以提供任何您想要的内容,具体取决于您希望在 PendingIntent
被触发时发生什么。 LocationManager
将向发送的 Intent
添加额外内容。这个 extra 具有 bundle 键 LocationManager.KEY_LOCATION_CHANGED
并且与该键关联的对象是一个 Location
对象。
LocationManager
将一次又一次地使用此 PendingIntent
来通知您的应用程序位置更新,所以我认为使用 PendingIntent.FLAG_ONE_SHOT
可能不是真是个好主意。如果您只想要一次更新,为什么不在获得一次更新后取消注册?
编辑:添加代码以在注册更新之前取消任何先前请求的更新
在调用 registerLocationUpdates()
之前,执行此操作以取消任何先前注册的更新:
Intent i = new Intent(this, LocationReceiver.class);
// Get any existing matching PendingIntent
PendingIntent pi = PendingIntent.getBroadcast(this, NOT_USED, i,
PendingIntent.FLAG_NO_CREATE);
if (pi != null) {
// Cancel any updates for this PendingIntent, because we are about to
// invalidate it
lm.removeUpdates(pi);
}
// Create a new PendingIntent and cancel any previous one
pi = PendingIntent.getBroadcast(this, NOT_USED, i,
PendingIntent.FLAG_CANCEL_CURRENT);
// Now register for location updates...
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME_BETWEEN_SCANS, MIN_DISTANCE, pi);
注意:实际上,我不知道为什么在这种情况下您需要取消任何以前的 PendingIntent
并创建一个新的。你可以得到一个PendingIntent
,如果你已经注册了那个PendingIntent
的位置更新,我不认为再次注册会导致PendingIntent
多次使用。如果您想尝试这样做,您需要做的就是删除 PendingIntent.FLAG_ONE_SHOT |来自现有代码的 PendingIntent.FLAG_CANCEL_CURRENT
。我认为这是更好/更清洁/更清晰的解决方案。
关于android - requestLocationUpdates with PendingIntent 和 Broadcast - 我得到什么广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16754896/
我在我的 AngularJS 应用程序中看到了一些非常奇怪的行为。 制作单个 $broadcast 时,(唯一的)接收器会被触发两次。 对整个应用程序的影响就好像接收器只被触发了一次。 (即只有一件元
以下是我的类(class)中的功能: def labeling(self, value, labelMap, dtype='string'): if dtype.value == 's
只是想澄清一些事情,一些容易实现的目标,一个通过在另一个问题中观察用户试图对广播变量调用 RDD 操作而产生的问题?这是错误的,对吧? 问题是:Spark 广播变量不是 RDD,对吗?这是 Scala
我正在尝试通过聊天室让聊天应用程序运行。 在服务器端我有类似的东西: io.sockets.on('connection', function (socket) { socket.emit('co
我目前正在基于此示例开发一个简单的聊天多房间: https://github.com/Atmosphere/atmosphere-samples/blob/master/samples/chat-mu
我正在构建一个搜索指令,我希望我的应用程序的多个其他指令能够监听对文本搜索的更改。 我试图了解广播和发射之间的区别,以及什么最适合我的目的。 据我了解,$broadcast 和 $emit 之间的区别
Class ProdsTransformer: def __init__(self): self.products_lookup_hmap = {} self.bro
我有多个名为 as 和 bs 的大型数据帧(大约 30GB),一个名为 spp 的相对较小的数据帧(大约 500MB ~ 1GB)。 我试图将 spp 缓存到内存中,以避免多次从数据库或文件中读取数据
我正在以下配置上运行 Spark 应用程序: 1 个 Master 节点,2 个 Worker 节点。 每个工作线程有 88 个核心,因此总数为 88 个。核心数量 176 每个工作线程有 502 G
我想检测 LAN 中 IP:10.x.x.x 掩码:255.0.0.0 的所有设备。我首先写了一个同步 ping 程序来逐个 ping IP,但是太慢了。因此我通过使用异步ping对其进行了改进,然后
我想检测 LAN 中 IP:10.x.x.x 掩码:255.0.0.0 的所有设备。我首先写了一个同步 ping 程序来逐个 ping IP,但是太慢了。因此我通过使用异步ping对其进行了改进,然后
张量(Tensor)、标量(scalar)、向量(vector)、矩阵(matrix) Python Numpy 切片和索引(高级索引、布尔索引、花式索引) Python NumPy
我试图找到这个词的起源,以及它是否有其他名称。我在网上能找到的所有用法都指向 numpy 手册。不知道以前是否使用过? 最佳答案 它最初源自 Yorick ,一种较旧的以数组为中心的编程语言,许多原始
有没有办法让 $broadcast 在初始化阶段将变量传播到 $on? {{testContent}} var app = angular
我可以要一个 $broadcast $on有多个参数, 就像是: $scope.$broadcast('event',$scope.item, $scope.item); 在任何情况下都有可能有这样的
尝试使用 Angular 的 $broadcast,当我测试它并使用字符串“hi”并在同级 Controller 中监视它时,它工作正常,如下所示: //first controller app.co
我遇到一个问题,事件已正确触发,但数据未从 Broadcast::channel 方法内的授权回调函数返回。 该事件如下所示: public function __construct($userId,
我们计划将 Telegram 与我们的项目集成。要求是当某些事件发生时,向一组用户发送通知。我已经用 Telegram 创建了一个测试机器人。我的问题是,是否可以使用机器人来做到这一点,即向用户组发送
我有一个简单的服务/工厂: angular.module('myapp').factory('User', ['$rootScope', function($rootScope) { retu
我们计划将 Telegram 与我们的项目集成。要求是当某些事件发生时,向一组用户发送通知。我已经用 Telegram 创建了一个测试机器人。我的问题是,是否可以使用机器人来做到这一点,即向用户组发送
我是一名优秀的程序员,十分优秀!