addProximityAlert 不起作用(requestLocationUpdates 也不起作用) - Android

发布于 2024-07-20 15:20:27 字数 1583 浏览 10 评论 0原文

我正在尝试在手机靠近某个位置时获取并更新。 我都尝试过使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

彡翼 2024-07-27 15:20:27

我认为问题在于你如何定义你的 Intent / PendingIntent。 使用 Intent 启动 Activity 有两种方法,您所包含的代码看起来像是这两种方法的交叉。

启动 Activity 的标准方法是使用 Intent 构造函数获取当前上下文和 Activity 的类,并使用 PendingIntent 上的 getActivity 方法创建 PendingIntent:

Intent intent = new Intent(this, ProximityAlert.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

或者,您可以将 IntentReceiver 添加到清单中的 Activity,并使用 IntentFilter 侦听特定操作(例如“eu.mauriziopz.gps.ProximityAlert”)。 但是,在这种情况下,您需要使用 PendingIntent.getBroadcast 来创建 PendingIntent。

Intent intent = new Intent("eu.mauriziopz.gps.ProximityAlert");
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

在所有情况下,您都需要确保您拥有清单中定义的基于位置的服务的正确权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

此外,您可以考虑使用静态常量 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 on PendingIntent to create the PendingIntent:

Intent intent = new Intent(this, ProximityAlert.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

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 use PendingIntent.getBroadcast to create the PendingIntent.

Intent intent = new Intent("eu.mauriziopz.gps.ProximityAlert");
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

In all cases you need to make sure you've got the correct permissions for location-based services defined in your manifest:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

Also, rather than using the string "gps", you might consider using the static constant LocationManager.GPS_PROVIDER.

百善笑为先 2024-07-27 15:20:27
Intent intent = new Intent("com.sccp.fourgdummy.REQUEST_DFGS");
intent.putExtra("launch_tab", "otp");
PendingIntent proximityIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

这很重要:Intent.FLAG_ACTIVITY_NEW_TASK

Intent intent = new Intent("com.sccp.fourgdummy.REQUEST_DFGS");
intent.putExtra("launch_tab", "otp");
PendingIntent proximityIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

this is important : Intent.FLAG_ACTIVITY_NEW_TASK

南渊 2024-07-27 15:20:27

API 将 addProximityAlert 的参数定义为 lat、long、radius、expiration、intent。 您是否尝试过将过期时间设置为 -1 而不是 100000


lm.addProximityAlert(12, 12, 100, 1000000, pIntent);

lm.addProximityAlert(12, 12, 100, -1, pIntent);

The API defines the parameters of addProximityAlert to be lat, long, radius, expiration, intent. Have you tried setting the expiration to -1 rather than 100000


lm.addProximityAlert(12, 12, 100, 1000000, pIntent);

lm.addProximityAlert(12, 12, 100, -1, pIntent);
各自安好 2024-07-27 15:20:27

我认为您不应该终止 onCreate 方法中已经存在的 Activity(通过调用 finish())。

I don't think you should terminate the Activity already in the onCreate method (by calling finish()).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文