OSM map tiles not displayed on Android version 8.1 - android

I have noticed that tiles are not showing on the map UI element after opening the activity. This is happening for new Android version 8.1.
Do you know what could be the problem?
Are there any new app permissions to declare?

The OSMDroid maps require External storage permission. Even if you add it to the manifest file the user needs to grant the permission during runtime manually if the api level > 23
#Override public void onCreate(Bundle savedInstanceState) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
//app has permissions, do your normal job here.
} else {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
And the request permission result
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//refresh view or recreate the Activity
}
}
}

Related

Android re-ask denied permission

I am using following code to request permission.
if (ContextCompat.checkSelfPermission(introduction_app.this,
Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(introduction_app.this,
Manifest.permission.READ_PHONE_STATE)) {
} else {
ActivityCompat.requestPermissions(introduction_app.this,
new String[]{Manifest.permission.READ_PHONE_STATE},
111);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults[0] == -1){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
finishAffinity();
else
finish();
}
}
But if user denied request then permission is not getting asked again. I am also not getting never ask again check box as other apps.
Can anyone please help me?
Thanx in advance,
Once user denies for first time, then it will display "never ask again"
if (ActivityCompat.shouldShowRequestPermissionRationale(introduction_app.this,
Manifest.permission.READ_PHONE_STATE)) {
ActivityCompat.requestPermissions(introduction_app.this,
new String[]{Manifest.permission.READ_PHONE_STATE},
111);
}

OnResquestPermissionResult from Adapter

I Have a BaseAdapter with a List of all requires permissions for use correctly the app.
I need to, after onClick in one of them, request the permission and check if is granted or not.
For request, I am using
ActivityCompat.requestPermissions
but I do not know how to get the results.
Is it possible to call O
nRequestpermissionsResultCallback
to know the answer of the user?
How can I do it?
so first i implement in the Adapter
implements ActivityCompat.OnRequestPermissionsResultCallback
then it require one Overide method
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
// it is not work here onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == storagePermission) {
// getPosts(false);
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context.getApplicationContext(), "Please allow permission to Continue", Toast.LENGTH_SHORT).show();
}
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context.getApplicationContext(), " Permissions Granted Succecfully", Toast.LENGTH_SHORT).show();
//here i load my method that i want to run
}
}
}
now problem is this onRequestPermissionsResult is not call from adapter instead
adapter triger onRequestPermissionsResult of the mainActivity Attach with the Adapter
so in main Activity i create this and run onRequestPermissionsResult of adapter by passing
adapterPosts.onRequestPermissionsResult(requestCode,permissions,grantResults);
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == storagePermission) {
// getPosts(false);
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
//permission not granted
}
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permission granted
adapterPosts.onRequestPermissionsResult(requestCode,permissions,grantResults);
//loadImageIntent();
}
}
}
To check the permission (in this example it's a permission to use the camera):
int permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA);
if (permissionCheck == PackageManager.PERMISSION_DENIED) {
// permission is not granted yet, let's ask for it
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
MyActivity.cameraRequestCode);
} else {
// permission is already granted
}
If ActivityCompat.requestPermissions is run, then a dialogue appears for the user, after which you can catch the result by overriding the Activity's onRequestPermissionsResult like this:
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MyActivity.cameraRequestCode: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// action when permission granted
} else {
// action when permission denied
}
return;
}
}
}
MyActivity.cameraRequestCode is a static int, can be any unique int value.
In your adapter class
class Adapter extends RecyclerView.Adapter<Your adapter.MyViewHolder> implements ActivityCompat.OnRequestPermissionsResultCallback{`//enter code here`}
It will override the onRequestPermissionsResult
I have a same problem in RecylclerView.Adapter and I use this section in permission request method
private void requestCallPhonePerm() {
if (ContextCompat.checkSelfPermission(context,
Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
onItemCallListener.itemCallPhone(itemPhones);
ActivityCompat.requestPermissions((Activity)context,
new String[]{Manifest.permission.CALL_PHONE},
MY_PERMISSIONS_REQUEST_CALL_PHONE);
} else {
((MainActivity)context).callPhone(itemPhones);
}
}
It's important to use (Activity)context in "requestPermissions" method to be invoked callback in container activity of your adapter
And in my MainActivity that contain recycler I use overrided request permission result normally.
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode){
case MY_PERMISSIONS_REQUEST_READ_CONTACTS:
if(grantResults.length>0
&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
new LoadContacts().execute();
}
break;
case MY_PERMISSIONS_REQUEST_CALL_PHONE:
if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
callPhone(phones);
}else{
}
}
}
I solved this from custom class. My custom class is derived from AppCompatActivity. Also you need to pass Activity context to your custom class and do whatever you need with it.
public bool AreStorageAndCamPermissionsGranted(Activity context)
{
if (Build.VERSION.SdkInt >= Build.VERSION_CODES.M)
if (
context.CheckSelfPermission(Manifest.Permission.Camera)!= Android.Content.PM.Permission.Granted
|| context.CheckSelfPermission(Manifest.Permission.ReadExternalStorage) != Android.Content.PM.Permission.Granted
|| context.CheckSelfPermission(Manifest.Permission.WriteExternalStorage) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(context, new String[]
{
Manifest.Permission.Camera,
Manifest.Permission.ReadExternalStorage,
Manifest.Permission.WriteExternalStorage,
}, REQUEST_PERMISSION_CODE);
}
return true;
}
`

How to request permission to open camera hardware

I trying to develop an App that uses the Camera hardware in Android. While coding the following line
this.mCameraManager.openCamera(cameraIds[0], new CameraDevice.StateCallback() {...}
I was asked to explicitly check if the permission is available. when i added the permission check, Android added the following lines
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != 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;
}
and I override the follwoing method
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
now, i do not know what to write inside the if-statement and the "onRequestPermissionsResult" mentioned above. your help is highly appreciated
requestPermissions(new String[]{Manifest.permission.CAMERA}, REQ_ACCESS_FINE_LOCATION);
You have to show android permission dialog to users, after that you can catch users motions in onRequestPermissionsResult.
I think that you should look this website : https://developer.android.com/training/permissions/requesting.html
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_CODE_PERMISSIONS:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED)
// Open Camera
break;
}
}
Try this:
// The permission required by the application to work properly
protected static final String[] requiredPermissions;
private static final int PERMISSION_REQUEST = 0;
static {
List<String> perms = new ArrayList<>(Arrays.asList(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO
));
requiredPermissions = perms.toArray(new String[perms.size()]);
}
Call verifyPermissions() method in onCreate():
private void verifyPermissions() {
if (!hasAllPermissions()) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
this,
requiredPermissions,
PERMISSION_REQUEST
);
}
}
private boolean hasAllPermissions() {
// Check if we have all required permissions.
for (String perm : requiredPermissions) {
if (ActivityCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST: {
// If request is cancelled, the result arrays are empty.
if (!hasAllPermissions()) {
finish();
}
return;
}
}
}

How get permission for camera in android.(Specifically Marshmallow)

Hey I am designing an app in android studio. In which i require permission of camera. I have included <uses-permission android:name="android.permission.CAMERA" />
in the the AndroidManifest.xml file. It is working properly in all versions of android except Marshmallow. How do I get camera permission by default? If not possible how do I ask it from user?
First check if the user has granted the permission:
if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED)
Then, you could use this to request to the user:
ActivityCompat.requestPermissions(activity, new String[] {Manifest.permission.CAMERA}, requestCode);
And in Marshmallow, it will appear in a dialog
You can try the following code to request camera permission in marshmallow. First check if the user grant the permission. If user has not granted the permission, then request the camera permission:
private static final int MY_CAMERA_REQUEST_CODE = 100;
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_REQUEST_CODE);
}
After requesting the permission, dialog will display to ask permission containing allow and deny options. After clicking action we can get result of the request with the following method:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
}
This works for me, the source is here
int MY_PERMISSIONS_REQUEST_CAMERA=0;
// Here, this is the current activity
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA))
{
}
else
{
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA );
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
check using this
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED)
and
I try to add following code:
private static final int MY_CAMERA_REQUEST_CODE = 100;
#RequiresApi(api = Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_REQUEST_CODE);
}
On onCreate Function and this following code:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
}
And this worked for me :)
One of the easiest way to take permission is to used the deterex library. like below
Implement the following library in your gradle.
implementation 'com.karumi:dexter:6.2.2'
after adding the library used this for permission like below
Dexter.withContext(this)
.withPermissions(
Manifest.permission.CAMERA
).withListener(new MultiplePermissionsListener() {
#Override public void onPermissionsChecked(MultiplePermissionsReport report) {/* ... */}
#Override public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {/*
... */}
}).check();
Don't forget to add the user permission in your manifest
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
Requesting Permissions
In the following code, we will ask for camera permission:
in java
EasyPermissions is a wrapper library to simplify basic system permissions logic when targeting Android M or higher.
Installation
EasyPermissions is installed by adding the following dependency to your build.gradle file:
dependencies {
// For developers using AndroidX in their applications
implementation 'pub.devrel:easypermissions:3.0.0'
// For developers using the Android Support Library
implementation 'pub.devrel:easypermissions:2.0.1'
}
private void askAboutCamera(){
EasyPermissions.requestPermissions(
this,
"A partir deste ponto a permissão de câmera é necessária.",
CAMERA_REQUEST_CODE,
Manifest.permission.CAMERA );
}
click here for full source code and learn more.
Before get permission you can check api version,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
}
else
{
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 401);
}
}
else
{
// if version is below m then write code here,
}
Get the Result of the permission dialog,
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 401) {
if (grantResults.length == 0 || grantResults == null) {
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openGallery();
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
}
} else if (requestCode == 402) {
if (grantResults.length == 0 || grantResults == null) {
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
}
}
}
private const val CAMERA_PERMISSION_REQUEST_CODE = 2323
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (checkSelfPermission(Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED)
{
requestPermissions(arrayOf(Manifest.permission.CAMERA),
CAMERA_PERMISSION_REQUEST_CODE)
}
}

Marshmallow won't show permission window

I have an app that needs write calendar now considered "critical" so I following the guidelines HERE I have added in Activity onCreate this cote
private static final int REQUEST_WRITE_CALENDAR = 1453;
...
boolean hasPermission = (ContextCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
ActivityCompat.requestPermissions(parentActivity,
new String[]{Manifest.permission.WRITE_CALENDAR},
REQUEST_WRITE_CALENDAR);
}
But this doesn't show any dialog.
In addition seems I should ad also this code block to handle response
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
{
case REQUEST_WRITE_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
//reload my activity with permission granted or use the features what required the permission
} else
{
Toast.makeText(parentActivity, "The app was not allowed to write to your calendar.", Toast.LENGTH_LONG).show();
}
}
}
}
The Android Target SDK is 23 and is correct. So the cause of the problem is different from that proposed in possible duplicate question.
The logic for determining permission status can be simplified to this:
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(parentActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_CALENDAR},
REQUEST_WRITE_CALENDAR);
}
Your Activity does not have to implement anything, overriding the onRequestPermissionsResult will be sufficient.
To the second part of the question:
You can override onRequestPermissionsResult in your activity or in your fragment. If you choose to do so in fragment instead of activity you should add something like this in your activity (in order to pass the callback to your fragment):
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
for (Fragment frag : getSupportFragmentManager().getFragments()) {
if (frag != null) {
frag.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}

Categories

Resources