addProximityAlert 不起作用(requestLocationUpdates 也不起作用) - Android
我正在尝试在手机靠近某个位置时获取并更新。 我都尝试过使用 addProximityAlert 和 requestLocationUpdates
LocationManager lm =(LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location l = lm.getLastKnownLocation("gps");
Intent intent = new Intent("eu.mauriziopz.gps.ProximityAlert");
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
lm.addProximityAlert(12, 12, 100, 1000000, pIntent);
这一个永远不会触发意图(我知道这是有效的,因为我可以手动启动它)。 我尝试使用侦听器,但它只执行一次。 无论我更新 GPS 多少次,它都不会再次被调用,
ProximityListener pl = new ProximityListener();
lm. requestLocationUpdates("gps", 2000, 10, pl);
这是活动的代码(在第一种情况下调用)
public class ProximityAlert extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.d("intent", "Hi");
finish();
}
这是监听器,
public class ProximityListener implements LocationListener {
String DEBUG_TAG = "ProximityListener";
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.d(DEBUG_TAG, location.toString());
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
谢谢
I'm trying to get and update when the phone gets near a location.
I've both tried using addProximityAlert and requestLocationUpdates
LocationManager lm =(LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location l = lm.getLastKnownLocation("gps");
Intent intent = new Intent("eu.mauriziopz.gps.ProximityAlert");
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
lm.addProximityAlert(12, 12, 100, 1000000, pIntent);
This one never fires the intent (which I know works since I can fire it up manually).
I tried using a listener but it gets executed only once. No matter how many times I update the gps it never gets called again
ProximityListener pl = new ProximityListener();
lm. requestLocationUpdates("gps", 2000, 10, pl);
this is the code of the activity (called in the first case)
public class ProximityAlert extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.d("intent", "Hi");
finish();
}
This is the Listener
public class ProximityListener implements LocationListener {
String DEBUG_TAG = "ProximityListener";
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.d(DEBUG_TAG, location.toString());
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为问题在于你如何定义你的 Intent / PendingIntent。 使用 Intent 启动 Activity 有两种方法,您所包含的代码看起来像是这两种方法的交叉。
启动 Activity 的标准方法是使用 Intent 构造函数获取当前上下文和 Activity 的类,并使用
PendingIntent
上的getActivity
方法创建 PendingIntent:或者,您可以将
IntentReceiver
添加到清单中的 Activity,并使用 IntentFilter 侦听特定操作(例如“eu.mauriziopz.gps.ProximityAlert”)。 但是,在这种情况下,您需要使用PendingIntent.getBroadcast
来创建 PendingIntent。在所有情况下,您都需要确保您拥有清单中定义的基于位置的服务的正确权限:
此外,您可以考虑使用静态常量
LocationManager.GPS_PROVIDER<,而不是使用字符串“gps” /代码>。
I think the problem is how you define you Intent / PendingIntent. There are two ways to start an Activity using an Intent, and the code you've included looks like a cross between the two.
The standard way of starting an Activity is to use the Intent constructor that takes the current context and the Activity's class and use the
getActivity
method onPendingIntent
to create the PendingIntent:Alternatively, you can add an
IntentReceiver
to your Activity in the Manifest, with an IntentFilter that listens for a particular action (like "eu.mauriziopz.gps.ProximityAlert"). However, in that case you need to usePendingIntent.getBroadcast
to create the PendingIntent.In all cases you need to make sure you've got the correct permissions for location-based services defined in your manifest:
Also, rather than using the string "gps", you might consider using the static constant
LocationManager.GPS_PROVIDER
.这很重要:
Intent.FLAG_ACTIVITY_NEW_TASK
this is important :
Intent.FLAG_ACTIVITY_NEW_TASK
API 将
addProximityAlert
的参数定义为 lat、long、radius、expiration、intent。 您是否尝试过将过期时间设置为-1
而不是100000
lm.addProximityAlert(12, 12, 100, 1000000, pIntent);
The API defines the parameters of
addProximityAlert
to be lat, long, radius, expiration, intent. Have you tried setting the expiration to-1
rather than100000
lm.addProximityAlert(12, 12, 100, 1000000, pIntent);
我认为您不应该终止 onCreate 方法中已经存在的 Activity(通过调用 finish())。
I don't think you should terminate the Activity already in the onCreate method (by calling finish()).