2016-08-03 10 views
3

AndroidでLocationListenerメソッドを使用して現在のデバイスの場所を取得しようとしています。一部の電話機では機能しないか、またはonlocationchangedが呼び出されません。そのような場合、どのように位置情報を取得するのですか? OnCreate関数で :onlocationchangedがAndroidで呼び出されていない

mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 0, this); 
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 

この

@Override 
    public void onLocationChanged(Location location) { 

     latitude = location.getLatitude(); 
     longitude = location.getLongitude(); 
     Log.e("TAG","lat and long "+latitude+" "+longitude); 
} 

    @Override 
    public void onStatusChanged(String s, int i, Bundle bundle) { 

    } 

    @Override 
    public void onProviderEnabled(String s) { 

    } 

    @Override 
    public void onProviderDisabled(String s) { 

    } 

しかしonLocationChangedは、このコードを使用マシュマロ

+0

位置の更新の呼び出し方法を表示できますか? –

+0

@PabloBaxterコード –

+0

を更新しました。これはマシュマロの下にあるすべての携帯電話に適用されますか? –

答えて

2

以下のバージョンで呼び出されていないが、すべてのデバイスで働いていたし、テスト:

private void getCurrentLocation() { 
//  Log.e("getLocation","Called"); 

     LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
     LocationListener locationListener = new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 
       mlat = location.getLatitude(); 
       mlon = location.getLongitude(); 
     String mLogn = Double.toString(mlon); 
       String mLatd = Double.toString(mlat); 
//    Log.e("mLogn",""+mLogn); 
//    Log.e("mLatd",""+mLatd); 
       mLogn=mLogn.trim(); 
       mLatd=mLatd.trim(); 
       if (mLatd==null || mLogn==null) { 
        if (mLatd.isEmpty()) { 
         mLatd = "No data found"; 
        } 
        if (mLogn.isEmpty()) { 
         mLogn = "No data found"; 
        } 
       } 

       // Log.e("Location",""+mlon+" "+mlat); 
//    Toast.makeText(CampaignTrain.this, "your location is " + mLogn + " " + mLatd, Toast.LENGTH_SHORT).show(); 

      } 

      @Override 
      public void onStatusChanged(String s, int i, Bundle bundle) { 

      } 

      @Override 
      public void onProviderEnabled(String s) { 

      } 

      @Override 
      public void onProviderDisabled(String s) { 

      } 
     }; 
     String locationProvide = LocationManager.NETWORK_PROVIDER; 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_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. 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
       requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},COARSE_LOCATION_REQUEST_CODE); 
       return; 
      } 


     } 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){ 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
       requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},FINE_LOCATION_REQUEST_CODE); 
       return; 
      } 

     } 
     locationManager.requestLocationUpdates(locationProvide, 0, 0, locationListener); 
     Location lastLocation=locationManager.getLastKnownLocation(locationProvide); 


    } 

実行時アクセス許可を設定します。

int FINE_LOCATION_REQUEST_CODE=101; 
int COARSE_LOCATION_REQUEST_CODE=102; 

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
      switch (requestCode) 
      { 

       case COARSE_LOCATION_REQUEST_CODE: 
       { 
        if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED) 
        { 

        } 
        else 
        { 
         mLatd="User denied location"; 
         mLogn="User denied location"; 
        } 
        return; 
       } 
       case FINE_LOCATION_REQUEST_CODE: 
       { 
        if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED) 
        { 

        } 
        else 
        { 
         mLatd="User denied location"; 
         mLogn="User denied location"; 
        } 
        return; 
       } 
      } 
     } 
+0

ありがとうございます。それを試してみましょう –

+0

場所を返しますが、onlocationchangedは私の古い場所を返します。私は自分の現在地が必要です。 –

+0

LocationManager locationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE)は、この行をlocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER、0、0、this)の下に追加します。 – Manish

関連する問題