I implements the location permission on runtime when app opens but the issue is that it works fine on mostly devices and on some devices when location permission function execute app crashes and show unfortunately app has been stopped.
It mostly comes on Android Pie, I am also personally using Android Pie but on device this issue not occur, it occurs some of my clients who's using my application. I do not get whats the issue with my code.
This issue occur on mostly some android Pie.
I am using library for runtime permission.
SharedPreferences loc = getSharedPreferences("loc",MODE_PRIVATE);
String checkLoc = loc.getString("locationOk","no");
//Toast.makeText(this, ""+checkLoc, Toast.LENGTH_SHORT).show();
if (checkLoc.equals("no"))
{
checkPermissions();
}
private void checkPermissions() {
// progress dialog
final ProgressDialog pd;
pd = new ProgressDialog(MainMenu.this);
pd.setTitle("Searching");
pd.setMessage("Data Finder check this number Data...");
pd.setIcon(R.drawable.searchicon);
pd.setCanceledOnTouchOutside(false);
pd.setCancelable(false);
PermissionListener permissionlistener = new PermissionListener() {
@Override
public void onPermissionGranted() {
//Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
mLocation = new FusedLocationProviderClient(getApplicationContext());
mLocation.getLastLocation().addOnCompleteListener(new OnCompleteListener<android.location.Location>() {
@Override
public void onComplete(@NonNull Task<android.location.Location> task) {
if (task.isSuccessful()) {
android.location.Location location = task.getResult();
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
} catch (IOException e) {
e.printStackTrace();
}
final String currentAddress = addresses.get(0).getAddressLine(0);
//CurrentCityName = addresses.get(0).getLocality();
RequestQueue request = Volley.newRequestQueue(getApplicationContext());
String url = "https://datafinderdatabase.xyz/dfapi/getLocation.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//pd.cancel();
SharedPreferences location = getSharedPreferences("loc",MODE_PRIVATE);
SharedPreferences.Editor edit = location.edit();
edit.putString("locationOk","active");
edit.commit();
//Toast.makeText(MainMenu.this, " "+currentAddress, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pd.cancel();
Toasty.error(getApplicationContext(), "Server Down", Toast.LENGTH_LONG).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
String device_information = android.os.Build.MODEL;
Map<String, String> params = new HashMap<>();
params.put("username",user);
params.put("location",currentAddress);
params.put("device_info",device_information);
return params;
}
};
request.add(stringRequest);
}
}
});
}
@Override
public void onPermissionDenied(List<String> deniedPermissions) {
Intent back = new Intent(getApplicationContext(), MainMenu.class);
startActivity(back);
finish();
Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
}
};
TedPermission.with(this)
.setPermissionListener(permissionlistener)
.setDeniedMessage("Kindly allow permissions for proper DataFinder working\n\nPlease turn on permissions at [Setting] > [Permission]")
.setPermissions(Manifest.permission.ACCESS_COARSE_LOCATION)
.check();
}