Ankur Jain Ankur Jain - 20 days ago 5
Android Question

Multiple Permissions

i have a java class, code as below.

public class PermisssionAction {

public static void requestPermission(Activity activity, String permission) {
if (ContextCompat.checkSelfPermission(activity, permission)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{permission}, 0);
}
}

}


Now i called its method in mainactivity to get permissions for app as below.

public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);

PermisssionAction.requestPermission(this, Manifest.permission.READ_PHONE_STATE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
PermisssionAction.requestPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
}
PermisssionAction.requestPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
PermisssionAction.requestPermission(this, Manifest.permission.READ_CONTACTS);
PermisssionAction.requestPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
PermisssionAction.requestPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
PermisssionAction.requestPermission(this, Manifest.permission.READ_SMS);
PermisssionAction.requestPermission(this, Manifest.permission.INTERNET);
PermisssionAction.requestPermission(this, Manifest.permission.BLUETOOTH);
PermisssionAction.requestPermission(this, Manifest.permission.ACCESS_WIFI_STATE);
PermisssionAction.requestPermission(this, Manifest.permission.SEND_SMS);



Intent startupintent = new Intent(MainActivity.this, StartupActivity.class);
startActivity(startupintent);
finish();


}
}


Now i m not able to understand why it ask only one permission while running app on real mobile, whereas it ask all permission in one go at emulator.
Also suppose app took location permission on first time , when u open app again it will ask another permission pending and so on on real device. can u guide.

Answer

First, only use requestPermissions() for dangerous permissions. INTERNET, for example, does not have a protectionLevel of dangerous.

Second, call requestPermissions() once, using a String[] of all the permissions that you are requesting. As it stands, you are calling requestPermissions() many times, with one permission per request.