为什么我没有拿到接近alterts即使我已经注册警报? [英] Why don't I get proximity alterts even though I've registered alerts?
问题描述
我想简单地设置一个接近后一个区域的测试,我只是添加到了我的主要活动的onCreate
方法。
I'm trying to simply set a proximity later for an area an for testing, I simply added this to the onCreate
method of my main activity.
public void onCreate(Bundle bndBundle) {
IntentFilter filter = new IntentFilter(WidgetService.ACTION_STOP_PROXIMITY);
registerReceiver(new ProximityIntentReceiver(), filter);
LocationManager locManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
Intent ittIntent = new Intent(this, ProximityIntentReceiver.class);
ittIntent.putExtra(WidgetService.KEY_STOP_IDENTIFIER, 1000);
PendingIntent pitIntent = PendingIntent.getBroadcast(this, 0, ittIntent, 0);
locManager.addProximityAlert(60.15769, 24.94150, 150, -1, pitIntent);
super.onCreate(bndBundle);
getActionBar().setDisplayHomeAsUpEnabled(false);
}
..这里是我使用简单的接收器类
..and here's the simple receiver class that I'm using
public class ProximityIntentReceiver extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 1000;
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
Log.d(getClass().getSimpleName(), "entering");
}
else {
Log.d(getClass().getSimpleName(), "exiting");
}
}
}
我对我的模拟器测试这一点,当我使用DDMS控制台手动设置手机的坐标,我还没有看到日志消息。
I'm testing this on my emulator and when I use the DDMS console to set the co-ordinates of the phone manually, I still don't see the log message.
我的清单文件没有任何特殊的code。我已经添加了正确的权限,并有code一个简单的活动 - 没有服务或任何东西。
My manifest file doesn't have any special code. I've added the correct permissions and have the code for a simple activity- no services or anything.
我经历了一大堆在计算器上的职位读,但我一直没能解决这个问题。我缺少的东西在我的代码片段?
I read through a whole bunch of posts on StacKOverflow but I haven't been able to resolve the issue. Am I missing something in my snippet?
推荐答案
您的动态注册这个接收器,通过 registerReceiver()
,有它的广播的行动作出回应字符串 WidgetService.ACTION_STOP_PROXIMITY
。
You are registering this receiver dynamically, through registerReceiver()
, to have it respond to broadcasts whose action string is WidgetService.ACTION_STOP_PROXIMITY
.
不过,您发送的实际广播试图用一个明确的意图
,确定您的接收器类。这不排队的IntentFilter
您使用的是与 registerReceiver()
。
However, the actual broadcast you are sending is trying to use an explicit Intent
, identifying your receiver class. This does not line up with the IntentFilter
that you are using with registerReceiver()
.
或者:
-
填写您的接收器清单,摆脱
registerReceiver的()
,在这种情况下,您明确意图
将工作或
Register your receiver in the manifest and get rid of
registerReceiver()
, in which case your explicitIntent
will work, or
使用新意图(WidgetService.ACTION_STOP_PROXIMITY)
而不是新意图(这一点,ProximityIntentReceiver.class)
,所以你的意图
与排队的的IntentFilter
Use new Intent(WidgetService.ACTION_STOP_PROXIMITY)
instead of new Intent(this, ProximityIntentReceiver.class)
, so your Intent
lines up with your IntentFilter
您不能使用明确的意图
对象发送广播通过 registerReceiver注册接收机()
。一个明确的意图
将只与清单登记的接收工作。
You cannot use explicit Intent
objects to send broadcasts to receivers registered via registerReceiver()
. An explicit Intent
will only work with a manifest-registered receiver.
这篇关于为什么我没有拿到接近alterts即使我已经注册警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!