如何让物体的位置,每10分钟的android [英] how to get Location object every 10 minutes in android

查看:87
本文介绍了如何让物体的位置,每10分钟的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请别人给我一个code可以恢复GPS位置,或每n分钟,我测试了几个code,但没有任何结果。

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

 公共final类TrackListener实现LocationListener的{

    私人最终语境mContext;
    公共布尔isGPSEnabled = FALSE;
    布尔isNetworkEnabled = FALSE;
    公共布尔canGetLocation = FALSE;

    地点位置; // 位置
    双纬度; // 纬度
    双经度; // 经度
    //最小距离改变以米为单位更新
    私有静态最后长MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; //10米
    //毫秒更新之间的最短时间
    私有静态最后长MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1分钟
    //声明一个位置管理
    保护LocationManager locationManager;

    公共TrackListener(上下文的背景下){
        this.mContext =背景;
        的getLocation();
    }
    公共场所的getLocation(){
        尝试 {
            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);

            如果(isGPSEnabled ==假放;&安培; isNetworkEnabled == FALSE){
            } 其他 {
                this.canGetLocation = TRUE;
                如果(isNetworkEnabled){
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
                    Log.d(网络,网络);
                    如果(locationManager!= NULL){
                        位置= locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        如果(位置!= NULL){
                            纬度= location.getLatitude();
                            经度= location.getLongitude();
                        }
                    }
                }
                如果(isGPSEnabled){
                    如果(位置== NULL){
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
                        Log.d(GPS功能的,GPS功能的);
                        如果(locationManager!= NULL){
                            位置= locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            如果(位置!= NULL){
                                纬度= location.getLatitude();
                                经度= location.getLongitude();
                            }
                        }
                    }
                }
            }

        }赶上(例外五){
            e.printStackTrace();
        }

        返回的位置;
    }
    公共无效stopUsingGPS(){
        如果(locationManager!= NULL){
            locationManager.removeUpdates(TrackListener.this);
        }
    }
    公共双getLatitude(){
        如果(位置!= NULL){
            纬度= location.getLatitude();
        }
        返回纬度;
    }
    公共双getLongitude(){
        如果(位置!= NULL){
            经度= location.getLongitude();
        }
        返回经度;
    }
    公共布尔canGetLocation(){
        返回this.canGetLocation;
    }
    公共无效showSettingsAlert(){
        AlertDialog.Builder alertDialog =新AlertDialog.Builder(mContext);
        alertDialog.setTitle(全球定位系统设置);
        alertDialog
                .setMessage(GPS未启用你想进入设置菜单。?);
        alertDialog.setPositiveButton(设置,
                新DialogInterface.OnClickListener(){
                    公共无效的onClick(DialogInterface对话,诠释它){
                        意向意图=新的意图(
                                ACTION_LOCATION_SOURCE_SETTINGS);
                        mContext.startActivity(意向);
                    }
                });
        alertDialog.setNegativeButton(取消,
                新DialogInterface.OnClickListener(){
                    公共无效的onClick(DialogInterface对话,诠释它){
                        dialog.cancel();
                    }
                });
        alertDialog.show();
    }

    公共无效onLocationChanged(位置定位){
        如果(位置!= NULL){
            纬度= location.getLatitude();
            经度= location.getLongitude();
        }
    }
    @覆盖
    公共无效onProviderDisabled(字符串提供商){
        Toast.makeText(Contexts.getAppContext(),GPS禁用,
                Toast.LENGTH_LONG).show();
    }
    @覆盖
    公共无效onProviderEnabled(字符串提供商){
        Toast.makeText(Contexts.getAppContext(),GPS功能的,
                Toast.LENGTH_LONG).show();
    }

    @覆盖
    公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
    }
 

但在课堂活动叫的getLocation()当我发现一个致命的问题

  8月4日至28号:41:55.126:W / dalvikvm(16004):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40015560)
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):致命异常:主要
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.example.testss / com.example.testss.MainActivity}:显示java.lang.NullPointerException
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):在android.os.Handler.dispatchMessage(Handler.java:99)
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):在android.os.Looper.loop(Looper.java:123)
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):在android.app.ActivityThread.main(ActivityThread.java:3683)
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):在java.lang.reflect.Method.invokeNative(本机方法)
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):在java.lang.reflect.Method.invoke(Method.java:507)
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):在dalvik.system.NativeStart.main(本机方法)
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):由:显示java.lang.NullPointerException
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):在com.example.testss.MainActivity.onCreate(MainActivity.java:27)
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    八月四号至28日:41:55.186:E​​ / AndroidRuntime(16004):11 ...更多
    八月四号至28日:46:55.255:I /流程(16004):发送信号。 PID:16004 SIG:9
 

解决方案

这可以回答两部分。

  • <一个href="http://stackoverflow.com/questions/16128091/setting-up-alarm-manager-is-creating-2-instances-of-my-main-activity/16134932#16134932">calling后台服务,每10分钟,使用报警
  • 使用LocationManager和LocationListener的,之间进行选择(或两者)网络和全球定位系统提供商。如果选择的网络供应商,因为GPS提供商需要一个地狱很长的时间去连接和我不需要GPS的精度。网络服务提供者是准确的高达250毫秒(我得到了在5个小时1不好的样品)。请记住LocationListener的,不会对三星手机的工作(因为三星手机已经高度定制构建Android的,他们不做移植的全面工作)。

  • 在我使用了一个的力量让我现在的位置还挺叫 God.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)

在除报警code以上,把下面的code在onHandleIntent

  @覆盖
最后的保护无效onHandleIntent(意向意图){
    mainContext = getApplicationContext();
    地点myLocation;
        如果(God.locationManager == NULL)
            God.locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);
        myLocation = God.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        如果(myLocation!= NULL)
            onLocationChanged(myLocation);
        其他 {
            God.notifications.setSpeed​​Notification();
        }
}
 

请注意:你可以选择LocationManager.GPS_PROVIDER,只是准备在露天进行测试(无云)约5分钟用4-5颗卫星同步

please someone give me a code can recover the GPS position or every n minutes, I tested several code but no result

Please someone give me a tutorial or a small program for that, I'm really stuck

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) {
    }

but when calling "getLocation()" in the class activity I find a fatal problem

 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

解决方案

This can be answered in 2 parts.

  • calling a background service every 10 mins, using alarm
  • Using a LocationManager and LocationListener, choosing between (or both) network and gps provider. If chose network provider, since gps provider takes a hell a long time to connect and I dont need the accuracy of gps. Network provider is accurate upto 250ms (i got 1 bad sample in 5 hours). Keep in mind LocationListener, does not work on Samsung phones (since samsung phones have heavily customized build of Android, and they dont do a thorough job of porting).

OR

  • I used a force get my location now kinda call God.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)

In addition to the alarm code above, put the following code in the 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();
        }
}

Note : You can choose LocationManager.GPS_PROVIDER, just be ready to test it under open sky (no clouds) and about 5 mins to sync with 4-5 satellites.

这篇关于如何让物体的位置,每10分钟的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆