我试图在我的项目中为棉花糖用户添加权限情况是:
我的设备需要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" />
答案 0 :(得分:0)
那是因为您正在检查if-else if statements中的权限,实际上一次只能执行一个满足的条件:
if (true) {
// only this one is executed
} else if (true) {
// does not execute
} else if (true) {
// does not execute
}
另外,我建议简化您的代码,因为您的问题是过度复杂的影响。 以下是您应该如何申请多项权限的示例: Android 6.0 multiple permissions
//编辑:在重新阅读您的问题后,我注意到您还询问了设备和模拟器之间的不同行为。这可能是因为您在两种环境中都有不同的已保存首选项和允许的首选项。请删除设备和模拟器上的应用数据并重置应用权限,然后重新检查。
答案 1 :(得分:0)
我通过使用三种不同的活动来完成它,并以完美而廉价的方式工作,但有一个解决方案:
如果用户检查,则还会检查是否正确实施了警报框 再也不要问
<强> PemrissionCamer.java 强>
import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.internal.view.ContextThemeWrapper;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class PermissionCamera extends Activity {
static int count=0;
int alert_time=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_permissioncalls);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(PermissionCamera.this,
new String[]{Manifest.permission.CAMERA},
3);
} else {
start_app();
}
}
else {
start_app2();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 3: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
count+=1;
if(count==1)
{
start_app();
}
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
Log.e("Camera3","Camera2");
// permission denied, boo! Disable the
// functionality that depends on this permission.
showAlert();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
public void start_app()
{
Intent i=new Intent(this,PermissionSms.class);
startActivity(i);
}
public void start_app2()
{
Intent i=new Intent(this,SplashScreen.class);
startActivity(i);
}
private void showAlert() {
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) {
quit();
dialog.dismiss();
finish();
}
});
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Allow",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
boolean showRationale = shouldShowRequestPermissionRationale( Manifest.permission.CAMERA );
if((! showRationale))
{
Log.e("Camera2","Camera2");
showAlert2();
}
else if (ContextCompat.checkSelfPermission(PermissionCamera.this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(PermissionCamera.this,
new String[]{Manifest.permission.CAMERA},
3);
dialog.dismiss();
}
}
});
alertDialog.show();
}
private void showAlert2() {
final String package_app="com.gwnt.flashlight.alert.call.and.sms";
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("You have checked 'Never ask again' enable permission manually!");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Quit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
quit();
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", package_app, null);
intent.setData(uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// quit();
dialog.dismiss();
}
});
alertDialog.show();
}
public void quit()
{
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
finish();
PermissionCamera.this.finish();
System.exit(0);
}
}
<强> PermissionSms.Java 强>
import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.internal.view.ContextThemeWrapper;
import android.view.Window;
import android.view.WindowManager;
import java.security.Permission;
public class PermissionSms extends Activity {
static int count=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_permissioncalls);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.RECEIVE_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(PermissionSms.this,
new String[]{Manifest.permission.RECEIVE_SMS},
1);
} else {
start_app();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
count+=1;
if(count==1)
{
start_app();
}
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
showAlert();
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
public void start_app()
{
Intent i=new Intent(this,PermisiionCall.class);
startActivity(i);
}
private void showAlert() {
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) {
quit();
dialog.dismiss();
finish();
}
});
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Allow",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
boolean showRationale = shouldShowRequestPermissionRationale( Manifest.permission.RECEIVE_SMS );
if((! showRationale))
{
showAlert2();
}
if (ContextCompat.checkSelfPermission(PermissionSms.this,
Manifest.permission.RECEIVE_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(PermissionSms.this,
new String[]{Manifest.permission.RECEIVE_SMS},
1);
dialog.dismiss();
}
}
});
alertDialog.show();
}
private void showAlert2() {
final String package_app="com.gwnt.flashlight.alert.call.and.sms";
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("You have checked 'Never ask again' enable permission manually!");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Quit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
quit();
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", package_app, null);
intent.setData(uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// quit();
dialog.dismiss();
}
});
alertDialog.show();
}
public void quit()
{
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
finish();
PermissionSms.this.finish();
System.exit(0);
}
}
<强> PermissionReceivePhone.java 强>
package com.gwnt.flashlight.alert.call.and.sms;
import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.internal.view.ContextThemeWrapper;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
public class PermisiionCall extends Activity {
static int count=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_permissioncalls);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(PermisiionCall.this,
new String[]{Manifest.permission.READ_PHONE_STATE},
2);
} else {
start_app();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 2: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
count+=1;
if(count==1)
{
start_app();
}
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
showAlert();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
public void start_app()
{
Intent i=new Intent(this,SplashScreen.class);
startActivity(i);
}
private void showAlert() {
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) {
quit();
dialog.dismiss();
finish();
}
});
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Allow",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
boolean showRationale = shouldShowRequestPermissionRationale( Manifest.permission.READ_PHONE_STATE );
if((! showRationale))
{
showAlert2();
}
else if (ContextCompat.checkSelfPermission(PermisiionCall.this,
Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(PermisiionCall.this,
new String[]{Manifest.permission.READ_PHONE_STATE},
2);
dialog.dismiss();
}
}
});
alertDialog.show();
}
private void showAlert2() {
final String package_app="com.gwnt.flashlight.alert.call.and.sms";
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("You have checked 'Never ask again' enable permission manually!");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Quit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
quit();
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", package_app, null);
intent.setData(uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// quit();
dialog.dismiss();
}
});
alertDialog.show();
}
public void quit()
{
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
finish();
PermisiionCall.this.finish();
System.exit(0);
}
}
如果允许,它将首先询问相机权限是否允许短信许可电话 如果允许,则
startapp 是移动到下一个活动的功能
即camera-&gt; sms-&gt; phone-&gt;功能