Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 写入设置权限不明确_Android_Android 6.0 Marshmallow - Fatal编程技术网

Android 写入设置权限不明确

Android 写入设置权限不明确,android,android-6.0-marshmallow,Android,Android 6.0 Marshmallow,我正在为android 6.0棉花糖构建我的应用程序,它需要写入设置权限。搜索之后,我知道,称之为: requestPermissions(new String[]{Manifest.permission.WRITE_SETTINGS}, 101); 不会显示对话框权限。所以,根据解决方案,我们应该检查Settings.System.canWrite() 返回true或false。因此,我应该将带有ACTION\u MANAGE\u WRITE\u设置的活动调用为A

我正在为android 6.0棉花糖构建我的应用程序,它需要写入设置权限。搜索之后,我知道,称之为:

requestPermissions(new String[]{Manifest.permission.WRITE_SETTINGS},
              101);
不会显示对话框权限。所以,根据解决方案,我们应该检查
Settings.System.canWrite()
返回true或false。因此,我应该将带有
ACTION\u MANAGE\u WRITE\u设置的活动调用为ACTION

但问题是,当我调用此活动时,它显示我的应用程序已被授予权限,尽管方法
Settings.System.canWrite()
返回false


我在这里遗漏了什么,或者我必须禁用它,然后再启用它。

我在为android 6开发时遇到了类似的问题。这是因为,现在开发人员必须在运行时请求权限。我的解决方案就在这里-

在onCreate中,显示权限对话框。假设该方法的名称为showPermissionsDialog()

//Global variable request code
private static final int WRITE_PERMISSION_REQUEST = 5000;

private void showPermissionsDialog() {
    if (Build.VERSION.SDK_INT == 23) {

      int hasWriteSettingsPermission = checkSelfPermission(Manifest.permission.WRITE_SETTINGS);
      if (hasWriteSettingsPermission != PackageManager.PERMISSION_GRANTED) {
        //You can skip the next if block. I use it to explain to user why I wan his permission.
        if (!ActivityCompat.shouldShowRequestPermissionRationale(HomeActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)) {
          showMessageOKCancel("You need to allow write settings",
              new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                  ActivityCompat.requestPermissions(HomeActivity.this, new String[]{Manifest.permission.WRITE_SETTINGS}, WRITE_PERMISSION_REQUEST);
                }
              });
          return;
        }
//The next line causes a dialog to popup, asking the user to allow or deny us write permission
        ActivityCompat.requestPermissions(HomeActivity.this, new String[]{Manifest.permission.WRITE_SETTINGS}, WRITE_PERMISSION_REQUEST);
        return;
      } else {
        //Permissions have already been granted. Do whatever you want :) 
      }
    }
}

//Now you only need this if you want to show the rationale behind  
//requesting the permission.      
    private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
    new AlertDialog.Builder(HomeActivity.this).setMessage(message).setPositiveButton("OK", okListener)
        .setNegativeButton("Cancel", null).show();
  }

//This method is called immediately after the user makes his decision to either allow 
  // or disallow us permision.
  @Override
  public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
      case WRITE_PERMISSION_REQUEST:
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
          //User pressed the allowed button
          //Do what you want :)
        } else {
          //User denied the permission
          //Come up with how to hand the requested permission
        }
      default:
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
  }

在使用Android 6.0.1(MMB29S)的Nexus 6上,以下代码:

if (!Settings.System.canWrite(this)) {
    Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
    intent.setData(Uri.parse("package:dummy"));
    startActivity(intent);
}
仅当“允许修改系统设置”设置为“禁用”时,才会打开设置。例如,在新安装后首次启动时(即不重新安装)

编辑(请参阅注释):某些设备可能与此代码有关,在这些设备中,
canWrite()
始终返回
false
,无论设置值如何

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (Settings.System.canWrite(context)) {

            //Write code to feature for eg. set brightness or vibrate device 
           /* ContentResolver cResolver = context.getContentResolver();
            Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS,brightness);*/
        }
        else {
            showBrightnessPermissionDialog(context);
        }
对话框:-

 private  static  void showBrightnessPermissionDialog(final Context context) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setCancelable(true);
    final AlertDialog alert = builder.create();
    builder.setMessage("Please give the permission to change brightness. \n Thanks ")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
                    intent.setData(Uri.parse("package:" + context.getPackageName()));
                   // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                    alert.dismiss();
                }
            });
    alert.show();
}
例如,完整的亮度代码

 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.net.Uri;
 import android.os.Build;
 import android.provider.Settings;
 import android.support.v7.app.AlertDialog;

  public class BrightnessHelper {

  public static void  setBrightness(Context context, int brightness){

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (Settings.System.canWrite(context)) {

      //Write code to feature for eg. set brightness or vibrate device
            ContentResolver cResolver = context.getContentResolver();  Settings.System.putInt(cResolver,  Settings.System.SCREEN_BRIGHTNESS,brightness);
        }
        else {
            showBrightnessPermissionDialog(context);
        }
    }

}

public static int getBrightness(Context context) {
    ContentResolver cResolver = context.getContentResolver();
    try {
        return Settings.System.getInt(cResolver,  Settings.System.SCREEN_BRIGHTNESS);
    } catch (Settings.SettingNotFoundException e) {
        return 0;
    }
}

private  static  void showBrightnessPermissionDialog(final Context context) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setCancelable(true);
    final AlertDialog alert = builder.create();
    builder.setMessage("Please give the permission to change brightness. \n Thanks ")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
                    intent.setData(Uri.parse("package:" + context.getPackageName()));
                   // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                    alert.dismiss();
                }
            });
    alert.show();
}


/*
    private boolean checkSystemWritePermission(Activity activity) {
        boolean retVal = true;
        if (Build.VERSION.SDK_INT >= activity.Build.VERSION_CODES.M) {
            retVal = Settings.System.canWrite(activity.getApplicationContext());
           // Log.d(TAG, "Can Write Settings: " + retVal);
            if(retVal){
                Toast.makeText(activity, "Write allowed :-)", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(this, "Write not allowed :-(", Toast.LENGTH_LONG).show();
                FragmentManager fm = getFragmentManager();
                PopupWritePermission dialogFragment = new PopupWritePermission();
                dialogFragment.show(fm, getString(R.string.popup_writesettings_title));
            }
        }
        return retVal;
    }*/
}

事实证明,如果在清单中声明了
CHANGE\u NETWORK\u STATE
,则即使未授予权限,允许
WRITE\u设置的切换也将默认为on位置。您甚至不需要声明
WRITE\u设置
就可以遇到此错误。

按如下方式编写此方法:

public void writePermission() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.System.canWrite(getApplicationContext())) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, 200);

        }
    }
}
然后在调用对话框之前调用该方法(writePermission)


我希望这有帮助

在请求许可之前,您是否检查是否已授予许可。如果已经授予,则无需再次请求授予权限。因为在marshmallow Forward上,用户可以通过在“设置”中手动拒绝权限。如果您需要如何检查权限是否已授予的代码,我们可以提供它。@Pankaj Commonware他已经解释了如果设置。系统。canWrite()返回false然后启动活动操作\u管理\u写入\u设置然后用户必须允许permissionSystem。canWrite()返回false,但权限页显示它已被授予@DhawalSodhaParmarI。我正在询问设置错误行为的原因。System.canWrite()。请参见,如果用户执行了以下操作,则不会向用户显示对话框-1。已经允许你了。2.拒绝你的允许。但是,如果用户是第一次安装此应用程序,则会向用户显示此信息。如果用户拒绝了您的权限,他仍然可以通过转到“设置”中的“应用权限”并选择相关权限来明确授予您权限。所以你可以决定去那里。我还没有探索意图部分,但我认为这是可能的。您是否尝试过:1)
adb安装
2)在设置中启用3)首次启动。顺便说一句,你们使用的是哪种设备和版本的Android?嗨,你们有什么结论吗?我现在遇到了同样的问题@Pankaj had,canWrite()总是返回false,除了“ACTION\u MANAGE\u WRITE\u SETTINGS”屏幕显示开关为ON之外。我找到的唯一解决办法是在开关中单击两次,因此值被重置,而canWrite()开始返回true,但这对用户管理来说是可怕的。我有类似的结论,在Moto X第二代6.0上,一切都很好,但Galaxy Note 5和Galaxy S6(均为6.0.1)正在再现此问题。三星默认启用写入系统设置,但调用settings.system.canWrite(此)时返回false。仅当再次禁用并启用该设置时,才会返回true。是的,另一个三星的错误。不正确的开关显示似乎是Android错误:谢谢。这应该是正确的答案。有解决办法吗?我还需要在我的应用程序中使用更改网络状态?我可以在运行时请求它,以便开关显示正确的位置吗?所有这些东西对我的下载来说都很糟糕。如果未授予权限,应用程序甚至不会启动:-(@Ton no CHANGE\u NETWORK\u STATE不需要运行时权限。它只需要在清单中声明