如何添加计时器以获取GPS位置?

时间:2018-04-29 04:40:17

标签: android gps location

我是初学者,我有两个问题: -

  1. 我用以下代码获取gps位置现在想要添加一个计时器来代码任何10分钟获取gps并保存文件GPS.json
  2. 如何共享文件gps.json以便在其他类这个项目中使用?
  3. 我测试的代码没问题,保存字符串使用的是Json格式。

    请帮帮我

      

    MyLocationListener类

        private class myLocationListener implements LocationListener{
    
        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            if(location!=null){
                locManager.removeUpdates(locListener);
    
                String longitude = "Longitude: " +location.getLongitude();
                String latitude = "Latitude: " +location.getLatitude();
                String altitiude = "Altitiude: " + location.getAltitude();
                String ACRY ="ACR:" + location.getAccuracy();
    
    
    
    
                try {
                    JSONObject jsonObject = new JSONObject();
                    JSONArray jsonArray = new JSONArray();
    
    
                    JSONObject record = new JSONObject();
    
                    record.put("longitude", longitude);
                    record.put("latitude", latitude);
                    record.put("altitiude", altitiude);
                    record.put("ACRY", ACRY);
    
                    jsonArray.put(record);
                    jsonObject.put("location", jsonArray);
    
                    File root = new File(Environment.getExternalStorageDirectory() + "/Android/test/data");
                    File gpxfile = new File(root((" Gps.json")));
                    FileOutputStream fileOutputStream = new FileOutputStream(gpxfile);
                    byte[] in = (jsonArray.toString().getBytes() );
                    fileOutputStream.write(in);
                    fileOutputStream.close();
                } catch (Exception e) {
                    Toast.makeText(context,e.getMessage(),Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        }
    
    
    
        @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
    
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

我提议你可以根据自己的需要改变。

    public void turnGPSOn() {

        String provider = Settings.Secure.getString(getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        if (!provider.contains("gps")) {
            final Intent poke = new Intent();
            poke.setClassName("com.android.settings",
                    "com.android.settings.widget.SettingsAppWidgetProvider");
            poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
            poke.setData(Uri.parse("3"));
            sendBroadcast(poke);
        }
    }


private class MyTimerTask extends TimerTask {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // code to get and send location information
                    locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    if (!locManager
                            .isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                        turnGPSOn();
                    }

                    try {
                        locManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER, 1000, 10,
                                locationListener);

                    } catch (Exception ex) {
                        turnGPSOff();
                    }

                }

            });
        }
    }

    private void updateWithNewLocation(Location location) {
        String latLongString = "";
        try {
            if (location != null) {

                Log.e("test", "gps is on send");
                latitude = Double.toString(location.getLatitude());
                longitude = Double.toString(location.getLongitude());

                Log.e("test", "location send");

                locManager.removeUpdates(locationListener);


                latLongString = "Lat:" + latitude + "\nLong:" + longitude;
                Log.w("CurrentLocLatLong", latLongString);
            } else {
                latLongString = "No location found";
            }
        } catch (Exception e) {
        }

    }

    private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            updateWithNewLocation(location);
        }

        public void onProviderDisabled(String provider) {
            updateWithNewLocation(null);
        }

        public void onProviderEnabled(String provider) {
        }

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

Calling the timer :

long gpsTimeInterval=2000;
void startTimer()
{

myTimer = new Timer();
                                myTimerTask = new MyTimerTask();
                                myTimer.scheduleAtFixedRate(myTimerTask, 0,
                                        gpsTimeInterval);
}

我希望你在android清单文件中添加互联网权限。