I\'ve an Android 6.0 Application with an unbound android service connecting to Google API for receiving Fused API Location Provider Updates. For this i need the ACCESS_FINE_LOCA
Use this method. This should work.
On Oncreate() :
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
givePerMisson();
} else {
requestIntent();
}
Add/Delete as per your need:
@TargetApi(Build.VERSION_CODES.M)
private void givePerMisson() {
if ((AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) &&
AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.CALL_PHONE) &&
AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) &&
AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.GET_ACCOUNTS) &&
AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) &&
AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.CAMERA)
)) {
requestIntent();
} else {
AndyUtils.givePermisson(SplashActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION, AndyUtils.PERMISSOIN);
AndyUtils.givePermisson(SplashActivity.this, Manifest.permission.CALL_PHONE, AndyUtils.PERMISSOIN);
AndyUtils.givePermisson(SplashActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, AndyUtils.PERMISSOIN);
AndyUtils.givePermisson(SplashActivity.this, Manifest.permission.ACCESS_FINE_LOCATION, AndyUtils.PERMISSOIN);
AndyUtils.givePermisson(SplashActivity.this, Manifest.permission.GET_ACCOUNTS, AndyUtils.PERMISSOIN);
AndyUtils.givePermisson(SplashActivity.this, Manifest.permission.CAMERA, AndyUtils.PERMISSOIN);
if (!AndyUtils.permissionsList.isEmpty()) {
requestPermissions(AndyUtils.permissionsList.toArray(new String[AndyUtils.permissionsList.size()]), AndyUtils.PERMISSOIN_CODE);
}
}
}
3.
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case AndyUtils.PERMISSOIN_CODE: {
if ((AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) &&
AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.CALL_PHONE) &&
AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) &&
AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.GET_ACCOUNTS) &&
AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) &&
AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.CAMERA)
)) {
requestIntent();
} else {
finish();
}
}
}
}
public static boolean checkPermission(Context context, String permission) {
try {
PackageManager pm = context.getPackageManager();
if (pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
}
} catch (Exception e) {
Log.e("PermissionError", e.toString());
}
return false;
}
5.
public static void givePermisson(Context context, String permisson, String permissonType) {
int per = context.checkSelfPermission(permisson);
if (per != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permisson);
} else if (per != PackageManager.PERMISSION_DENIED) {
}
}
For that you have to check before your service start if your service start before checking permission will not work properly
Yes you can accept the permissions once in activity and use it in service , if you grant the permission , then it will never ask again. Following is the code for showing dialog for ACCESS_FINE_LOCATION
,You can also refer Requesting Permissions at Run Time
private void showPermissionDialog() {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(mActivity,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an expanation 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.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(mActivity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_FOR_LOCATION);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
if (mGoogleApiClient != null)
mGoogleApiClient.connect();
}
}