我想在我的項目中添加權限棉花糖用戶的情況下工作的多個權限是:
我需要3個權限我的設備的攝像頭,手機短信,電話安卓:在模擬器而不是在其他一些設備
當應用程序運行時只顯示一個許可,其他兩個權限得到了跳過,
,我閉上我的應用程序,並重新打開它,它表明第二權限和第三一個跳過,然後再次關閉它,打開它第三個權限顯示如此。
在嘗試它模擬器它完美的所有權限3顯示了在一次,但不是在設備(即三星J5,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" />