我試圖激活使用彈出我的設備,而不去設置頁面我的設備上的位置;我嘗試了多種堆棧溢出解決方案,但似乎沒有任何工具可以打開我的模擬器或設備位置。
我的位置代碼如下:模擬器位置
OnRequest:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull
String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
permission. its an array becayse it can be multiple permissions
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0 , 0, locationListener);
}
}
}
主營:
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i("location",""+location.toString());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION} ,1);
}else{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0 , 0, locationListener);
}
清單:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
建設搖籃:
compile 'com.android.support:support-v4:25.0.0'
compile 'com.google.android.gms:play-services-maps:10.2.4'
compile 'com.google.android.gms:play-services-location:10.2.4'
嘿,我試圖將這段代碼仍然是位置沒有被我的代碼打開。如果你打算使用你的工作室和模擬器,這是否會打開,在你身邊? –
您無法通過模擬器中的GPS獲取當前位置。在設備中運行應用程序以確保它可以正常工作。 –
啊是的,我試圖在兩者還是一樣的結果無法爲它打開programmaticly –