Location Services Work But Not Once The User Grants Permission - java

So I used a tutorial and pretty much copied and only changed a little if the user grants permission the app works after the user leaves the app and comes back. If they open the app and grant permission nothing happens, also if they deny permission nothing happens.
public class MainActivity extends AppCompatActivity {
private ArrayList<Weather> weatherLsit;
public static int REQUEST_PERMISSION=1;
private RecyclerView recyclerView;
private EditText zipCodeEditText;
LocationDataService locationDataService = new LocationDataService(MainActivity.this, this);
FusedLocationProviderClient fusedLocationProviderClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.hourly_recycle_view);
weatherLsit = new ArrayList<>();
weatherLsit.add(new Weather(10, 6, R.drawable.cloudy_icon));
weatherLsit.add(new Weather(15, 7, R.drawable.cloudy_icon));
weatherLsit.add(new Weather(10, 8, R.drawable.cloudy_icon));
weatherLsit.add(new Weather(60, 9, R.drawable.cloudy_icon));
weatherLsit.add(new Weather(80, 10, R.drawable.cloudy_icon));
weatherLsit.add(new Weather(20, 12, R.drawable.cloudy_icon));
weatherLsit.add(new Weather(13, 12, R.drawable.cloudy_icon));
setAdapter();
}
#Override
protected void onStart() {
super.onStart();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationDataService.getLastLocation();
} else {
locationDataService.requestLocationPermission();
}
}
private void setAdapter() {
WeatherAdapter adapter = new WeatherAdapter(weatherLsit);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION) {
if (permissions.length >=0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
System.out.println("PERMISSON HAS BEEN GRANTED!");
//do something permission is allowed here....
} else {
Toast.makeText(this, "Please allow the Permission", Toast.LENGTH_SHORT).show();
}
}
}
}
for some reason, the onRequestPermissionsResult isn't being executed I've spent a couple of hours already on this problem, and for some reason, I cant get it to work.

You can try putting the permission check in the onCreate() method.
like following:
And change the permission code to the following:
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
Log.i(TAG, "sdk < 28 Q");
if (ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
String[] strings =
{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
ActivityCompat.requestPermissions((Activity)context, strings, 1);
}
} else {
if (ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context,
"android.permission.ACCESS_BACKGROUND_LOCATION") != PackageManager.PERMISSION_GRANTED) {
String[] strings = {Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
"android.permission.ACCESS_BACKGROUND_LOCATION"};
ActivityCompat.requestPermissions((Activity)context, strings, 2);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults.length > 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "onRequestPermissionsResult: apply LOCATION PERMISSION successful");
} else {
Log.i(TAG, "onRequestPermissionsResult: apply LOCATION PERMISSSION failed");
}
}
if (requestCode == 2) {
if (grantResults.length > 2 && grantResults[2] == PackageManager.PERMISSION_GRANTED
&& grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "onRequestPermissionsResult: apply ACCESS_BACKGROUND_LOCATION successful");
} else {
Log.i(TAG, "onRequestPermissionsResult: apply ACCESS_BACKGROUND_LOCATION failed");
}
}
}

Related

error ArrayIndexOutOfBoundsException occurs only sometimes, user permission?

Some users in my app get this error:
Caused by: java.lang.ArrayIndexOutOfBoundsException: at
com.file.gt.MainActivity.onRequestPermissionsResult
(MainActivity.java:234) at android.app.Activity.requestPermissions
(Activity.java:5323) at com.file.gt.MainActivity.onCreate
(MainActivity.java:85) at android.app.Activity.performCreate
(Activity.java:8114) at android.app.Activity.performCreate
(Activity.java:8098) at
android.app.Instrumentation.callActivityOnCreate
(Instrumentation.java:1309) at
android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:3480)
It occurs only sometimes, with some users.
I have this in my app:
private final String [] permissions = {
"android.permission.RECORD_AUDIO",
"android.permission.READ_PHONE_STATE"
};
int requestCode = 200;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(permissions[0]) == PackageManager.PERMISSION_DENIED ||
checkSelfPermission(permissions[1]) == PackageManager.PERMISSION_DENIED) {
requestPermissions(permissions, requestCode);
}
}
and this:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
recreate(); // 234
}
else {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle(getString(R.string.alert_title));
alertDialog.setMessage(getString(R.string.alert_text));
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
(dialog, which) -> dialog.dismiss());
alertDialog.show();
}
}
Any ideas what is wrong and what I'm doing wrong?
Thanks.
When you get an ArrayIndexOutOfBoundsException it means you are accessing an array index outside the array size. Your stack trace says it happens in onRequestPermissionsResult, and the only array you access there is grantResults.
What can sometimes happen is that grantResults is empty in onRequestPermissionsResult if the user declines the permissions, so you should check its size before accessing its members
if(grantResults.length == 2 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED &&
grantResults[1] == PackageManager.PERMISSION_GRANTED) {
recreate();
}
else {
// handle permissions not granted
}
Alternately, if you use the new registerForActivityResult approach you can get around this entirely. Have a look here for more info, or here for an example use.
private final String [] permissions = {
android.Manifest.permission.RECORD_AUDIO,
android.Manifest.permission. READ_PHONE_STATE,
};
int requestCode = 200;
public static boolean hasPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;}
//user this code to check
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}else{
recreate();
}

Why isn't ActivityCompat.requestPermissions prompting a permission request?

I'm trying to make an activity request for location related permissions by using the code below but when I run the code, it doesn't ask the user for permission for some reason. I added the permissions in the manifest file for both coarse location and fine location. When I open up the activity, it doesn't request the user to enable locations related permissions. I'm not sure why that is and I would appreciate any help.
final int COARSE_LOCATION = 99, FINE_LOCATION = 100;
...
#Override
protected void onStart() {
super.onStart();
if (ContextCompat.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.
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, FINE_LOCATION);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, FINE_LOCATION);
}
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, COARSE_LOCATION);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, COARSE_LOCATION);
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case FINE_LOCATION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLastLocation();
} else {
Toast.makeText(ContactListPage.this,"wrong permission",Toast.LENGTH_SHORT).show();
}
}
}
private void getLastLocation() {
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;
}
Task<Location> locationTask = fusedLocationProviderClient.getLastLocation();
locationTask.addOnSuccessListener(new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
Toast.makeText(ContactListPage.this,"Lat: " + latitude,Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(ContactListPage.this,"location was null",Toast.LENGTH_SHORT).show();
}
}
});
locationTask.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(ContactListPage.this,"error getting last location",Toast.LENGTH_SHORT).show();
}
});
}

Requesting permissions in Android

I am trying to request 2 permissions when the app opens up. It works fine, but when the user selects never ask again, a toast message should be displayed once. The toast message keeps on coming up repeatedly and does not stop. I cannot figure out the reason for this.
public void checkPermission(final Context context) {
if(ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.CAMERA)) {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
}
else {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
}
}
}
#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 && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Both permissions granted", Toast.LENGTH_SHORT).show();
} else {
checkPermission(this);
}
if (requestCode == 1) {
for (int i = 0, len = permissions.length; i < len; i++) {
String permission = permissions[i];
boolean showRationale;
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
showRationale = shouldShowRequestPermissionRationale(permission);
if (!showRationale) {
Toast.makeText(this, "Denied", Toast.LENGTH_SHORT).show();
}
}
}
}
}
Above is the code that checks for permissions.
I would like to know why the toast message never stops popping up.
Try following code -
public class MainActivity extends AppCompatActivity{
private static final int STORAGE_PERMISSION_REQ_CODE = 1001;
private static final int CAMERA_PERMISSION_REQ_CODE = 1002;
private static final int BOTH_PERMISSION_REQ_CODE = 1003;
...........
private void checkPermission(){
if(hasBothPermission()){
// Has both permission
}
else if(hasReadStoragePermission(READ_EXTERNAL_STORAGE))
// Has Storage Permission request Camera Permission
requestPermissions(new String[]{CAMERA}, CAMERA_PERMISSION_REQ_CODE);
else if(hasCameraPermission(CAMERA))
// Has Camera Permission request Storage Permission
requestPermissions(new String[]{READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_REQ_CODE);
else
// Request Both Permission
requestPermissions(new String[]{READ_EXTERNAL_STORAGE, CAMERA},BOTH_PERMISSION_REQ_CODE);
}
private boolean hasBothPermission(){
return (ContextCompat.checkSelfPermission(this, CAMERA) == PackageManager.PERMISSION_GRANTED) &&
ContextCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}
private boolean hasCameraPermission(String perm) {
return (ContextCompat.checkSelfPermission(this, perm) == PackageManager.PERMISSION_GRANTED);
}
private boolean hasReadStoragePermission(String perm) {
return (ContextCompat.checkSelfPermission(this, perm) == PackageManager.PERMISSION_GRANTED);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if(requestCode == STORAGE_PERMISSION_REQ_CODE){
if(hasReadStoragePermission(READ_EXTERNAL_STORAGE))
Toast.makeText(this, "Storage Permission Granted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Storage Permission Failed, Request Again", Toast.LENGTH_SHORT).show();
}
else if(requestCode == CAMERA_PERMISSION_REQ_CODE){
if(hasCameraPermission(CAMERA))
Toast.makeText(this, "Camera Permission Granted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Camera Permission Failed, Request Again", Toast.LENGTH_SHORT).show();
}
else {
if(hasBothPermission())
Toast.makeText(this, "Permission Granted Success", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Permission Failed Request Permission Again", Toast.LENGTH_SHORT).show();
}
}
...........
}

Permission Not Granted

I'm trying to get access to the system settings, but it seems my method that requests the permission always returns false. My knowledge is limited so I don't really know where to start to fix it
private void getPermissions(){
boolean value;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
value = Settings.System.canWrite(getApplicationContext());
if(value) {
success = true;
}else{
Intent intent = new
Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" +
getApplicationContext().getPackageName()));
startActivity(intent);
}
}
}
the getPermission() is called upon the creation of the app.
Try this
String[] permissionsRequired = new String{Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO};
//on button click
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if ((ContextCompat.checkSelfPermission(this, permissionsRequired[0])
!= PackageManager.PERMISSION_GRANTED) ||
(ContextCompat.checkSelfPermission(this, permissionsRequired[1])
!= PackageManager.PERMISSION_GRANTED) ||
(ContextCompat.checkSelfPermission(this, permissionsRequired[2])
!= PackageManager.PERMISSION_GRANTED)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermission();
}
}
}
}
break;
}
}
public void requestPermission() {
ActivityCompat.requestPermissions((Activity) this, permissionsRequired, REQUEST_PERMISSION_SETTING);
}

Granting Android Permissions - unknown value

Some time ago i added permission to my app about 'fine location' by some tutorial from internet. Everything works like i want, but i have question about 1 unexplained line:
private static final int MY_PERMISSION_REQUEST_FINE_LOCATION = 101;
Why is there value 101 ? Why not 102 or 200 ?
private static final int MY_PERMISSION_REQUEST_FINE_LOCATION = 101;
.
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_REQUEST_FINE_LOCATION);
} else {
}
return;
}
.
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSION_REQUEST_FINE_LOCATION:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
LatLng position = new LatLng(0, 0);
} else {
close();
}
}
}
You can give it the value you want.It's worth doing the job onRequestPermissionResult to catch the requestCode
In the documentation of requestPermissions(), it says that the requestCode is for
Application specific request code to match with a result reported to
onRequestPermissionsResult(int, String[], int[]). Should be >= 0.
What it means is that you'll explicitly specify this code to any values that you want, and you can then use this value to check if your request is successful or not in the callback onRequestPermissionsResult. An example, put this code in your Activity:
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 101:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//granted
} else {
//not granted
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
So back to your question, you can use 102, or 200, or whatever value you would like. But you should always check if the request is successful or not, by using the same code that you provide in the requestPermissions method.

Categories