【问题标题】:how to get Location object every 10 minutes in android如何在android中每10分钟获取一次位置对象
【发布时间】:2013-04-28 09:30:29
【问题描述】:

请给我一个代码可以恢复 GPS 位置或每隔 n 分钟,我测试了几个代码但没有结果

请有人给我一个教程或一个小程序,我真的卡住了

public final class TrackListener implements LocationListener {

    private final Context mContext;
    public boolean isGPSEnabled = false;
    boolean isNetworkEnabled = false;
    public boolean canGetLocation = false;

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude
    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
    // Declaring a Location Manager
    protected LocationManager locationManager;

    public TrackListener(Context context) {
        this.mContext = context;
        getLocation();
    }
    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(Contexts.LOCATION_SERVICE);
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            Log.v("isGPSEnabled", "=" + isGPSEnabled);
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            Log.v("isNetworkEnabled", "=" + isNetworkEnabled);

            if (isGPSEnabled == false && isNetworkEnabled == false) {
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }
    public void stopUsingGPS() {
        if (locationManager != null) {
            locationManager.removeUpdates(TrackListener.this);
        }
    }
    public double getLatitude() {
        if (location != null) {
            latitude = location.getLatitude();
        }
        return latitude;
    }
    public double getLongitude() {
        if (location != null) {
            longitude = location.getLongitude();
        }
        return longitude;
    }
    public boolean canGetLocation() {
        return this.canGetLocation;
    }
    public void showSettingsAlert() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
        alertDialog.setTitle("GPS is settings");
        alertDialog
                .setMessage("GPS is not enabled. Do you want to go to settings menu?");
        alertDialog.setPositiveButton("Settings",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(
                                "ACTION_LOCATION_SOURCE_SETTINGS");
                        mContext.startActivity(intent);
                    }
                });
        alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        alertDialog.show();
    }

    public void onLocationChanged(Location location) {
        if (location != null) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
        }
    }
    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(Contexts.getAppContext(), "GPS Disable ",
                Toast.LENGTH_LONG).show();
    }
    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(Contexts.getAppContext(), "GPS enabled",
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

但是在课堂活动中调用“getLocation()”时,我发现了一个致命问题

 04-28 08:41:55.126: W/dalvikvm(16004): threadid=1: thread exiting with uncaught exception (group=0x40015560)
    04-28 08:41:55.186: E/AndroidRuntime(16004): FATAL EXCEPTION: main
    04-28 08:41:55.186: E/AndroidRuntime(16004): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testss/com.example.testss.MainActivity}: java.lang.NullPointerException
    04-28 08:41:55.186: E/AndroidRuntime(16004):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
    04-28 08:41:55.186: E/AndroidRuntime(16004):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    04-28 08:41:55.186: E/AndroidRuntime(16004):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    04-28 08:41:55.186: E/AndroidRuntime(16004):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    04-28 08:41:55.186: E/AndroidRuntime(16004):    at android.os.Handler.dispatchMessage(Handler.java:99)
    04-28 08:41:55.186: E/AndroidRuntime(16004):    at android.os.Looper.loop(Looper.java:123)
    04-28 08:41:55.186: E/AndroidRuntime(16004):    at android.app.ActivityThread.main(ActivityThread.java:3683)
    04-28 08:41:55.186: E/AndroidRuntime(16004):    at java.lang.reflect.Method.invokeNative(Native Method)
    04-28 08:41:55.186: E/AndroidRuntime(16004):    at java.lang.reflect.Method.invoke(Method.java:507)
    04-28 08:41:55.186: E/AndroidRuntime(16004):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    04-28 08:41:55.186: E/AndroidRuntime(16004):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    04-28 08:41:55.186: E/AndroidRuntime(16004):    at dalvik.system.NativeStart.main(Native Method)
    04-28 08:41:55.186: E/AndroidRuntime(16004): Caused by: java.lang.NullPointerException
    04-28 08:41:55.186: E/AndroidRuntime(16004):    at com.example.testss.MainActivity.onCreate(MainActivity.java:27)
    04-28 08:41:55.186: E/AndroidRuntime(16004):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    04-28 08:41:55.186: E/AndroidRuntime(16004):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    04-28 08:41:55.186: E/AndroidRuntime(16004):    ... 11 more
    04-28 08:46:55.255: I/Process(16004): Sending signal. PID: 16004 SIG: 9

【问题讨论】:

  • 你有访问GPS的权限吗?
  • 是的,我做了
  • 您在 MainActivity 第 27 行中有一个 NullPointerException。与您发布的代码无关。正确初始化您的对象。
  • 了解如何使用调试器,在这种情况下确定错误的位置非常有用。

标签: android android-location


【解决方案1】:

这可以分为两部分来回答。

  • calling a background service every 10 mins, using alarm
  • 使用 LocationManager 和 LocationListener,在(或两者)网络和 gps 提供商之间进行选择。如果选择网络提供商,因为 gps 提供商需要很长时间才能连接,而且我不需要 gps 的准确性。网络提供商的准确度高达 250 毫秒(我在 5 小时内得到了 1 个坏样本)。请记住 LocationListener,它不适用于三星手机(因为三星手机有大量定制的 Android 版本,并且它们没有彻底完成移植工作)。

  • 我使用了现在强制获取我的位置有点像电话God.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)

除了上面的报警代码,在onHandleIntent里面放如下代码

@Override
final protected void onHandleIntent(Intent intent) {
    mainContext = getApplicationContext() ;
    Location myLocation;
        if (God.locationManager == null)
            God.locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        myLocation = God.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (myLocation != null)
            onLocationChanged(myLocation);
        else {
            God.notifications.setSpeedNotification();
        }
}

注意:您可以选择 LocationManager.GPS_PROVIDER,准备好在开阔的天空(无云)下测试它,大约 5 分钟即可与 4-5 颗卫星同步。

【讨论】:

  • 准备好放入大量的Log调试语句,在多部手机上疯狂调试。位置不容易顺利工作。需要时间和精力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-11
  • 1970-01-01
相关资源
最近更新 更多