2016-10-22 102 views
0

如果我理解正確的話,在此我的代碼onLocationChanged方法應該會自動在每個1米叫,或0.4秒,因爲我已經設置locationManager.requestLocationUpdates(provider, 400, 1, this);onLocationChanged()將不會自動調用

但沒有任何反應!當我按下按鈕時,onLocationChanged被調用。沒有自動調用,既不是每1秒也不是0.4秒。任何人都可以幫忙嗎?

public class MainActivity extends AppCompatActivity implements LocationListener { 
LocationManager locationManager; 
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99; 
String provider; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    provider = locationManager.getBestProvider(new Criteria(),false); 

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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; 
    } 
    Location location = locationManager.getLastKnownLocation(provider); 

    if (location != null) { 

     Log.i("Location Info", "Location achieved!"); 
     Toast.makeText(getApplicationContext(),"Location achieved!",Toast.LENGTH_SHORT).show(); 

    } else { 

     Log.i("Location Info", "No location :("); 
     Toast.makeText(getApplicationContext(),"No location :(",Toast.LENGTH_SHORT).show(); 

    } 

} 
public boolean checkLocationPermission() { 
    if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission. ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission. ACCESS_FINE_LOCATION)) { 

      // Show an explanation to the user *asynchronously* -- don't block 
      // this thread waiting for the user's response! After the user 
      // sees the explanation, try again to request the permission. 
      new AlertDialog.Builder(this) 
        .setTitle("Title") 
        .setMessage("Text") 
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i) { 
          //Prompt the user once explanation has been shown 
          ActivityCompat.requestPermissions(MainActivity.this, 
            new String[]{Manifest.permission. ACCESS_FINE_LOCATION}, 
            MY_PERMISSIONS_REQUEST_LOCATION); 
         } 
        }) 
        .create() 
        .show(); 


     } else { 
      // No explanation needed, we can request the permission. 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission. ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_LOCATION); 
     } 
     return false; 
    } else { 
     return true; 
    } 
} 
@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_LOCATION: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! Do the 
       // location-related task you need to do. 
       if (ContextCompat.checkSelfPermission(this, 
         Manifest.permission. ACCESS_FINE_LOCATION) 
         == PackageManager.PERMISSION_GRANTED) { 

        //Request location updates: 
        locationManager.requestLocationUpdates(provider, 400, 1, this); 
       } 

      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 

      } 
      return; 
     } 

    } 
} 



@Override 
protected void onResume() { 
    super.onResume(); 

    checkLocationPermission(); 


} 

@Override 
protected void onPause() { 
    super.onPause(); 

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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; 
    } 
    locationManager.removeUpdates(this); 

} 

@Override 
public void onLocationChanged(Location location) { 

    Double lat = location.getLatitude(); 
    Double lng = location.getLongitude(); 

    Log.i("Location info: Lat", lat.toString()); 
    Log.i("Location info: Lng", lng.toString()); 
    Toast.makeText(getApplicationContext(),"Location info: Lat"+lat.toString(),Toast.LENGTH_SHORT).show(); 
    Toast.makeText(getApplicationContext(),"Location info: Lng"+lng.toString(),Toast.LENGTH_SHORT).show(); 

} 

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

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) { 

} 


public void getLocation(View view) { 

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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; 
    } 
    Location location = locationManager.getLastKnownLocation(provider); 

    onLocationChanged(location); 
} 
} 
+1

如果您的應用程序已具有ACCESS_FINE_LOCATION權限,'locationManager.requestLocationUpdates(...)'將永遠不會被調用。 – Titus

回答

1

只要修改onResume()方法覆蓋以請求位置更新(如果權限已被授予)。

如果權限未被授予,requestLocationUpdates()調用將發生在onRequestPermissionsResult()方法覆蓋(如果用戶接受權限請求)。

如果此權限已被授予,您可以立即繼續並立即致電requestLocationUpdates()

@Override 
protected void onResume() { 
    super.onResume(); 

    if (checkLocationPermission()) { 
     locationManager.requestLocationUpdates(provider, 400, 1, this); 
    } 
} 
+0

還是什麼都沒有..我開始的應用程序,並注意ha手only節,只有當我按下按鈕,顯示敬酒信息拉特和日誌...一般情況下發生的事情自動? – bojan

+0

出門走走,onLocationChanged回調應該在你四處移動時調用。 –

+0

我會去嘗試..更多的事情,我只有ACCESS_FINE_LOCATION清單,是好的,或者我必須有coarse_location? – bojan