Granting Android Permissions - unknown value - java

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.

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();
}
});
}

Location Services Work But Not Once The User Grants Permission

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");
}
}
}

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();
}
}
...........
}

onLocationChanged() won't call automatically

If I understand correctly,in this my code onLocationChanged method should be called automatically on every 1m,or 0.4sec,as I've set locationManager.requestLocationUpdates(provider, 400, 1, this);
But nothing happens! When I press button, onLocationChanged is called. Nothing automatically called, neither on every 1 second nor on 0.4sec. Can anybody help?
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);
}
}
Just modify your onResume() method override to request location updates if the permission has already been granted.
If the permission has not been granted, the requestLocationUpdates() call will happen in the onRequestPermissionsResult() method override (if the user accepts the permission request).
If the permission has previously been granted, you can just go ahead and call requestLocationUpdates() immediately.
#Override
protected void onResume() {
super.onResume();
if (checkLocationPermission()) {
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
}

Categories