I am trying to add permission in my project for marshmallow user the situation is:
I need 3 permissions for my device Camera, SMS, Phone Calls
When application runs only one permission is shown and the other two permissions got skipped ,
and I close my app and re-open it it shows second permission and the 3rd one skipped, and closed it again and opened it the 3rd permission is shown so on.
While trying it on Emulator it works perfectly all 3 permissions are shown at once but not in Device(i.e Samsung j5, Samsung j7):
Permission.Java
public class PermissionActivity extends Activity {
int count = 0;
public static final String CAMERA_PREF = "camera_pref";
public static final String SMS_PREF = "sms_pref";
public static final String CALL_PREF = "call_pref";
public static final int MY_PERMISSIONS_REQUEST_CAMERA = 100;
public static final int MY_PERMISSIONS_REQUEST_RECIEVE_SMS = 101;
public static final int MY_PERMISSIONS_REQUEST_PHONE = 102;
public static final String ALLOW_KEY = "ALLOWED";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_permission);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
chk_all_permissions();
} else {
// do something for phones running an SDK before lollipop
start_app();
}
}
// Example for sms permissions
public static Boolean getFromPref(Context context, String key) {
SharedPreferences myPrefs = context.getSharedPreferences(CAMERA_PREF,
Context.MODE_PRIVATE);
Log.e("Key", String.valueOf(key));
return (myPrefs.getBoolean(key, false));
}
public static Boolean getFromPrefSms(Context context, String key) {
SharedPreferences myPrefs = context.getSharedPreferences(SMS_PREF,
Context.MODE_PRIVATE);
Log.e("Key", String.valueOf(key));
return (myPrefs.getBoolean(key, false));
}
public static Boolean getFromPrefCalls(Context context, String key) {
SharedPreferences myPrefs = context.getSharedPreferences(CALL_PREF,
Context.MODE_PRIVATE);
Log.e("Key", String.valueOf(key));
return (myPrefs.getBoolean(key, false));
}
private void chk_all_permissions() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.RECEIVE_SMS)
!= PackageManager.PERMISSION_GRANTED) {
marshmallow_permissions_sms();
} else if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
marshmallow_permission_camera();
} else if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
marshmallow_permission_phone();
}
}
// Example for camera permissions
private void marshmallow_permission_camera() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
if (getFromPref(this, ALLOW_KEY)) {
customPermDialogue();
} else if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
Log.e("Key", "CAMERA");
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
ActivityCompat.requestPermissions(PermissionActivity.this,
new String[]{Manifest.permission.CAMERA,},
MY_PERMISSIONS_REQUEST_CAMERA);
Log.e("Key5", "SCAmera");
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA,},
MY_PERMISSIONS_REQUEST_CAMERA);
Log.e("Key2", "SCAmera");
}
}
}
}
private void marshmallow_permissions_sms() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.RECEIVE_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (getFromPrefSms(this, ALLOW_KEY)) {
Log.e("Key", "SMS2");
customPermDialogue();
} else if (ContextCompat.checkSelfPermission(this,
Manifest.permission.RECEIVE_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
Log.e("Key", "SMS");
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.RECEIVE_SMS)) {
ActivityCompat.requestPermissions(PermissionActivity.this,
new String[]{Manifest.permission.RECEIVE_SMS,},
MY_PERMISSIONS_REQUEST_RECIEVE_SMS);
Log.e("Key5", "SMS");
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.RECEIVE_SMS,},
MY_PERMISSIONS_REQUEST_RECIEVE_SMS);
Log.e("Key2", "SMS");
}
}
}
}
// Example for phone permissions
private void marshmallow_permission_phone(){
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
if (getFromPrefCalls(this, ALLOW_KEY)) {
Log.e("Key", "CALL2");
showSettingsAlert();
} else if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
Log.e("Key", "CALL");
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_PHONE_STATE)) {
ActivityCompat.requestPermissions(PermissionActivity.this,
new String[]{Manifest.permission.READ_PHONE_STATE,},
MY_PERMISSIONS_REQUEST_PHONE);
Log.e("Key5", "PHONE");
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_PHONE_STATE,},
MY_PERMISSIONS_REQUEST_PHONE);
Log.e("Key2", "CALL");
}
}
}
}
public void showSettingsAlert() {
AlertDialog alertDialog = new AlertDialog.Builder(PermissionActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("App needs to access the Camera.");
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "DONT ALLOW",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "ALLOWAnce",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
chk_all_permissions();
dialog.dismiss();
}
});
alertDialog.show();
}
// A dialogue to show if permission is declined
private void customPermDialogue() {
AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AppTheme)).create();
alertDialog.setTitle("Alert");
alertDialog.setCancelable(false);
alertDialog.setIcon(R.drawable.icon);
alertDialog.setMessage("App needs to access required Permissions !");
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Don't Allow",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Allow",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
chk_all_permissions();
}
});
alertDialog.show();
}
public void start_app()
{
Intent i=new Intent(this,SplashScreen.class);
startActivity(i);
}
public void showCustomAlert(String msg)
{
Toast toast = Toast.makeText(getApplicationContext(),
msg,
Toast.LENGTH_SHORT);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
v.setTextColor(Color.parseColor("#25456d"));
View toastView = toast.getView();
toastView.setBackgroundResource(R.drawable.toastdrawable);
toast.show();
}
Mainifest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />