2017-12-25 24 views
2

私は初心者のAndroidアプリケーションの開発です。私はSQLiteのDBへの送信のために5分ごとには、次のステップでGPSTracker.classから緯度経度を取得しますが、私はGPSTracker.class
を呼び出すときには、緯度を返します= 0.0経度= 0.0すぎサービスの緯度と経度が返ってきた0.0

これは私のコードです

私を助けてください

AppService.class 

    public class AppService extends Service { 
    private MyThread thread; 
    PowerManager.WakeLock wl; 
    double latitude; 
    double longitude; 

    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     thread = new MyThread(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     if (!thread.isAlive()) {  
      thread.start(); 
     } 
     return START_STICKY; 
    } 

    private class MyThread extends Thread { 
     private static final String tag = "Sevice Demo"; 
     private static final int delay = 300000; 
     private int roundNumber = 0; 
     private boolean finishService = false; 

     @Override 
     public void run() { 
      while (true) { 
       PowerManager pm = (PowerManager) getApplicationContext().getSystemService(
         getApplicationContext().POWER_SERVICE); 
       wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); 
       wl.acquire(); 
       wl.release(); 

       GPSTracker gps = new GPSTracker(AppService.this); 
       latitude = gps.getLatitude(); 
       longitude = gps.getLongitude(); 
       Log.d("dddd",String.valueOf(latitude)+" & "+String.valueOf(longitude)); 

       try { 
        sleep(delay); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       if (finishService) { 
        return; 
       } 
      } 
     } 
    } 

    @Override 
    public void onDestroy() { 
     Toast.makeText(this, "Service Stop...", Toast.LENGTH_LONG).show(); 
     if (thread.isAlive()) {  
      stopService(new Intent(this, AppService.class)); 
     } 
     super.onDestroy(); 
    } 

    public class GPSTracker extends Service implements LocationListener { 
     private final Context mContext; 
     boolean isGPSEnabled = false;   
     boolean isNetworkEnabled = false;  
     boolean canGetLocation = false;  
     Location locations; 

     private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; 
     private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; 
     protected LocationManager locationManager;   

     public GPSTracker(Context context) { 
      this.mContext = context; 
      getLocation(); 
     } 

     public Location getLocation() { 
      try { 
       locationManager = (LocationManager) mContext 
         .getSystemService(LOCATION_SERVICE); 

       // get GPS status 
       isGPSEnabled = locationManager 
         .isProviderEnabled(LocationManager.GPS_PROVIDER); 

       // get network status 
       isNetworkEnabled = locationManager 
         .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
       if (!isGPSEnabled && !isNetworkEnabled) { 
        // no network provider is enabled 
       } else { 
        this.canGetLocation = true; 
        if (isNetworkEnabled) { 
         if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
          // TODO: Consider calling 
          // ActivityCompat#requestPermissions 
          // here to request the missing permissions, and then overriding 
          // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
          //           int[] grantResults) 
          // to handle the case where the user grants the permission. See the documentation 
          // for ActivityCompat#requestPermissions for more details. 
          //return TODO; 
         } 
         locationManager.requestLocationUpdates(
           LocationManager.NETWORK_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("Network", "Network"); 
        } 
        if (locationManager != null) { 
         locations = locationManager 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        } 
        if (locations != null) { 
         latitude = locations.getLatitude(); 
         longitude = locations.getLongitude(); 
        } 

       } 
       // if GPS Enabled get lat/long using GPS Services 
       if (isGPSEnabled) { 
        if (locations == 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) { 
          locations = locationManager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (locations != null) { 
           latitude = locations.getLatitude(); 
           longitude = locations.getLongitude(); 
          } 
         } 
        } 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return locations; 
     } 

     public double getLatitude() { 
      if (locations != null) { 
       latitude = locations.getLatitude(); 
      } 
      return latitude; 
     } 

     public double getLongitude() { 
      if (locations != null) { 
       longitude = locations.getLongitude(); 
      } 
      return longitude; 
     } 

     public boolean canGetLocation() { 
      return this.canGetLocation; 
     } 

     @Nullable 
     @Override 
     public IBinder onBind(Intent intent) { 
      return null; 
     } 

     @Override 
     public void onLocationChanged(Location location) { 

     } 

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

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    } 

} 

enter image description here

+0

get locationの取得にgoogleApiClientを使用すると、networkProviderが建物エリアで正しく機能しません。 –

+0

マニフェストファイルも追加してください –

+1

正確な位置を取得するには、 'FusedLocationProviderAPI'を使用してください。 – Piyush

答えて

1

融合ロケーションプロバイダでは、Googleでの位置情報APIをインテリジェントにそのアプリのニーズ位置情報を提供するために、異なる信号を組み合わせたサービスを再生しています。

融合ロケーションプロバイダは、GPSとWi-Fiなどの根本的な場所技術を管理し、あなたが必要なサービス品質を指定するために使用できるシンプルなAPIを提供します。たとえば、利用可能な最も正確なデータを要求することも、消費電力を追加することなく最高の精度を要求することもできます。

Last Known Location

Receiving Location Updates

+0

すべては、これらの2つのリンクで詳しく説明されています。 – Remario

+0

使用しているコードは古くなっていますので、位置データの測定と更新にはお勧めしません。 – Remario

+0

ありがとうございます:) –

0

リファクタリングする必要が

このコード

locationManager.requestLocationUpdates(
      LocationManager.GPS_PROVIDER, 
      MIN_TIME_BW_UPDATES, 
      MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
    Log.d("GPS Enabled", "GPS Enabled"); 
    if (locationManager != null) { 
     locations = locationManager 
       .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if (locations != null) { 
      latitude = locations.getLatitude(); 
      longitude = locations.getLongitude(); 
     } 
    } 
LocationManager.requestUpdates()に電話するときは、 LocationManagerにGPS位置の更新があったときにあなたのアプリを呼び戻すように求めています。これは、電話機がGPSを使用して位置を決定するのにかかる時間に応じて、後で(将来の)後に行われます。 LocationManager.getLastKnownLocation()への呼び出しは、まだ持っていない最後の「既知の」位置のみを返します。

位置情報の更新をリクエストすると、GPSデータが返されます。フレームワークはメソッドonLocationChanged()を呼び出します。そのメソッドには現在コードがありません。したがって、メソッドが呼び出されたときに何もしません。