Reputation: 381
according osmdroid wiki I added "dangerous" permissions at runtime
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (hasPermissions()) {
initMap();
} else {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (hasPermissions()) {
initMap();
} else {
finish();
}
}
}
but for first time when I run my app and give permission location and other needed permission my map does not appear but for second time when I run my app , map is appear? I know this problem is for permission but where is the my permission problem ?
private boolean hasPermissions() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED;
}
this is my manifest permission:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Upvotes: 1
Views: 878
Reputation: 1
I'm having the same issue, I found the following code snippet from osmdroid official wiki, and It's work for me. (I'm using version 6.0.1)
Try to put the following code when Activity or Fragment onCreate():
@Override public void onCreate(Bundle savedInstanceState) {
Context ctx = getApplicationContext();
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
...
setContentView(R.layout.activity_main);
}
After grant "dangerous" permissions in runtime, Recreate the Activity or Fragment. Then the mapView will works normally without reopen the APP.
Upvotes: 0
Reputation: 381
I put :
setContentView(R.layout.activity_main);
into the initMap(), the problem gone.
Upvotes: 2
Reputation: 453
Try to put a few seconds delay before calling initMap() in onRequestPermissionsResult()
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (hasPermissions()) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
initMap();
}
}, 3000);
} else {
finish();
}
}
The updated permissions sometimes take time in reflecting into the code.
Upvotes: 1