为什么我的 GPS 会“挂断”?

发布于 2024-12-09 06:58:37 字数 2037 浏览 0 评论 0原文

我正在尝试实现一个 GPS 位置侦听器,该侦听器将在后台保持开启并不断更新。我意识到这是不好的做法,但我这样做是为了测试目的。现在我有一个通过 getGPS() 调用的位置侦听器。也可以使用killGPS() 来杀死它。

当我四处走动并单击“获取 GPS”按钮时,它可以完美地工作,该按钮从我获得的位置参数中获取纬度和经度。

    GPSActivity.getGPS();
    loc_listener = GPSActivity.loc_listener;
     locationManager = (LocationManager)
     getSystemService(LOCATION_SERVICE);
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
     0, 0, loc_listener);
    location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

但是在后台,有时它会说我仍然在我的公寓,即使我一直在外出3小时! 为什么我的位置侦听器不会自行更新,即使它每 10 分钟就会打开一次以查找新位置?

谢谢

package com.cellphone;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class GPSActivity extends Activity{
    public static LocationListener loc_listener = null;

    public static void getGPS() {
        if (loc_listener == null) {
            loc_listener = new LocationListener() {

                @Override
                public void onStatusChanged(String provider, int status,
                        Bundle extras) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderEnabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderDisabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onLocationChanged(Location location) {
                    // TODO Auto-generated method stub

                }
            };
        }

    }

    public static void killGPS(LocationManager locationManager) {
        if (locationManager != null && loc_listener != null) {
            locationManager.removeUpdates(loc_listener);
        }
    }

}

I am trying to implement a gps location listener that will stay on and continuously update in the background. I realize this is bad practice but I am doing this for testing purposes. Right now I have a location listener that gets called with getGPS(). It can also be killed with killGPS().

It works perfectly when im walking around and I click my get gps button which takes the latitude and longitude from my location parameter which I get from

    GPSActivity.getGPS();
    loc_listener = GPSActivity.loc_listener;
     locationManager = (LocationManager)
     getSystemService(LOCATION_SERVICE);
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
     0, 0, loc_listener);
    location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

However in the background, sometimes it will say I'm still at my apartment even though I've been out for 3hours!
Why is my location listener not updating itself even though it turns on to find a new location every 10 mins?

Thanks

package com.cellphone;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class GPSActivity extends Activity{
    public static LocationListener loc_listener = null;

    public static void getGPS() {
        if (loc_listener == null) {
            loc_listener = new LocationListener() {

                @Override
                public void onStatusChanged(String provider, int status,
                        Bundle extras) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderEnabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderDisabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onLocationChanged(Location location) {
                    // TODO Auto-generated method stub

                }
            };
        }

    }

    public static void killGPS(LocationManager locationManager) {
        if (locationManager != null && loc_listener != null) {
            locationManager.removeUpdates(loc_listener);
        }
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

美煞众生 2024-12-16 06:58:37

我建议仅使用 Activity 来显示信息,并在后台使用持有部分唤醒锁的服务,这样即使设备尝试进入睡眠模式,它也能继续运行。然后在活动中使用广播接收器从服务获取位置更新。

无论您如何管理它,您都需要持有唤醒锁以确保它继续运行。

检查此链接:
http://developer.android.com/reference/android/os/PowerManager.html

您必须持有权限:

<uses-permission android:name="android.permission.WAKE_LOCK" />

对于唤醒锁:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");

//you would acquire in onStart() or on your getGPS() or somewhere else if you like.
mWakeLock.acquire();
// you would release in your onStop() or your onDestroy() or on your killGPS()
mWakeLock.release();

I would recommend only using the Activity to display information and use a service in the background that holds a partial wake lock so it continues to run even if the device tries to go into sleep mode. Then use a broadcastreceiver in the Activity to get updates from the service with the location.

Regardless of how you manage it you need to hold a wake lock to ensure it continues running.

Check this Link:
http://developer.android.com/reference/android/os/PowerManager.html

You must hold permission:

<uses-permission android:name="android.permission.WAKE_LOCK" />

And for the wake lock:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");

//you would acquire in onStart() or on your getGPS() or somewhere else if you like.
mWakeLock.acquire();
// you would release in your onStop() or your onDestroy() or on your killGPS()
mWakeLock.release();
走野 2024-12-16 06:58:37

对于位置轮询器,这里很简单&工作示例:
https://github.com/commonsguy/cwac-locpoll

您可以通过以下方式保持应用程序的状态在应用程序上下文中添加函数。也就是说,位置轮询器可以了解应用程序状态,并在应用程序未处于活动状态时跳过 GPS 操作。

for location poller, here is simple & working example:
https://github.com/commonsguy/cwac-locpoll

you can keep state of your app by adding a function in Application context. So to say, location poller can be aware of app-state and skip gps action if app is not active.

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