多次许可后启动活动?android 6+

时间:2017-03-09 04:34:06

标签: android runtime-permissions

我已经在我的启动画面中获得了Req Multiple权限

public class Main_MulPer extends Activity {

public static final int R_PERM = 321;

Context context = this;

public static boolean hasPermissions(Context context, String... permissions) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rcssa);


    int PERMISSION_ALL = 1;
    String[] PERMISSIONS = {Manifest.permission.CAMERA,
    Manifest.permission.READ_PHONE_STATE,
    Manifest.permission.INTERNET,
    Manifest.permission.ACCESS_NETWORK_STATE,
    Manifest.permission.ACCESS_WIFI_STATE,
    Manifest.permission.NFC,
    Manifest.permission.READ_EXTERNAL_STORAGE,
    Manifest.permission.WRITE_EXTERNAL_STORAGE,     
    };

    if (!hasPermissions(this, PERMISSIONS)) {
        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);

        Main_MulPer.this.finish();
        Intent ss = new Intent(Main_MulPer.this, Main_acti.class);
        ss.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        ss.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ss.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        ss.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(ss);

    } else {
        if (!hasPermissions(this, PERMISSIONS)) ;
        {
            ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Main_MulPer.this.finish();
                    Intent i = new Intent(Main_MulPer.this, Splash_two.class);
                    startActivity(i);
                }
            }, 3000);

        }
    }

}

}

所以这里我的问题是它一次要求两个权限......

如果我试图让他们转移到另一个活动......所以我给了类似的3个活动,每个活动有2个权限..

但由于Handler正在开展新的活动.. 然后我删除了延迟处理程序...现在它打开最后一个活动......直接......

任何人都可以建议我如何在所有权限之后启动主要活动... 没有许可它应退出应用程序...请帮助我

更新

Insted of Multiple permissions我已经分配了3个活动,每个活动有两个权限......但是这里它应该在获得许可之后转到下一个活动但是每次前两个活动权限丢失时它将持续到最后一个活动 所以我需要退出app并给他们......

我需要的是,未经许可,不要移动到下一个屏幕

任何人都可以在获得许可后建议我转移到下一个活动....

3 个答案:

答案 0 :(得分:7)

试试这个,

 public class Main_nPRC extends Activity {

public static final String MainPP_SP = "MainPP_data";
public static final int R_PERM = 2822;
private static final int REQUEST= 112;

Context mContext = this;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rcssa);

    SharedPreferences settings = getSharedPreferences(MainPP_SP, 0);
    HashMap<String, String> map = (HashMap<String, String>) settings.getAll();

    if (Build.VERSION.SDK_INT >= 23) {
        Log.d("TAG","@@@ IN IF Build.VERSION.SDK_INT >= 23");
        String[] PERMISSIONS = {android.Manifest.permission.CAMERA,
                android.Manifest.permission.READ_PHONE_STATE,
                android.Manifest.permission.INTERNET,
                android.Manifest.permission.ACCESS_NETWORK_STATE,
                android.Manifest.permission.ACCESS_WIFI_STATE,
                android. Manifest.permission.NFC,
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
        };


        if (!hasPermissions(mContext, PERMISSIONS)) {
            Log.d("TAG","@@@ IN IF hasPermissions");
            ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
        } else {
            Log.d("TAG","@@@ IN ELSE hasPermissions");
            callNextActivity();
        }
    } else {
        Log.d("TAG","@@@ IN ELSE  Build.VERSION.SDK_INT >= 23");
        callNextActivity();
    }

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case REQUEST: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d("TAG","@@@ PERMISSIONS grant");
                callNextActivity();
            } else {
                Log.d("TAG","@@@ PERMISSIONS Denied");
                Toast.makeText(mContext, "PERMISSIONS Denied", Toast.LENGTH_LONG).show();
            }
        }
    }
}

private static boolean hasPermissions(Context context, String... permissions) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

public void callNextActivity()
{
    Intent ss = new Intent(Main_nPRC.this, NMainSS.class);
    ss.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    ss.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    ss.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    ss.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    startActivity(ss);
    finish();
}



@Override
public void onBackPressed() {

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setTitle("ⓘ Exit ! " + getString(R.string.app_name));
    alertDialogBuilder
            .setMessage(Html.fromHtml("<p style='text-align:center;'>Please Fill the required details</p><h3 style='text-align:center;'>Click Yes to Exit !</h4>"))
            .setCancelable(false)
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            moveTaskToBack(true);
                            android.os.Process.killProcess(android.os.Process.myPid());
                            System.exit(0);

                        }
                    })

            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    dialog.cancel();
                }
            });

    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();

}

}

答案 1 :(得分:1)

final int PERMISSION_REQUEST_CODE = 111;

if (Build.VERSION.SDK_INT >= 23) {
      if (!checkReadContactPermission() ||!checkReadPhoneStatePermission()
          || !checkWriteExternalStorage() || !checkReadExternalStorage() ||
          !checkSystemAlertWindowPermission() || !checkWriteContactPermission()) {
                   requestPermission();
            } else {
                  // Move to main act
          }
      } else {
                  // Move to main act
   }

您必须为每个

制作检查权限的方法

代表。这是READ CONTACT,同样添加所有其他

private boolean checkReadContactPermission() {
            int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_CONTACTS);
            if (result == PackageManager.PERMISSION_GRANTED) {
                return true;
            } else {
                return false;
            }
        }

请求权限的方法

private void requestPermission() {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_CONTACTS,
                            Manifest.permission.WRITE_CONTACTS,
                            Manifest.permission.READ_PHONE_STATE,
                           /* Manifest.permission.CAMERA,*/
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE},
                    PERMISSION_REQUEST_CODE);
        }

最后是RequestPermissionResult

   @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                // if granted then 0 else -1
                // i have 5 permisson to check so 0,1,2,3,4..
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED &&
                        grantResults[1] == PackageManager.PERMISSION_GRANTED &&
                        grantResults[2] == PackageManager.PERMISSION_GRANTED &&
                        grantResults[3] == PackageManager.PERMISSION_GRANTED &&
                        grantResults[4] == PackageManager.PERMISSION_GRANTED) {

                    // means all permission are granted..move to Main activity

                } else {
                   // show alert
                }
                break;
        }
    }

答案 2 :(得分:0)

当您申请权限时,您将收到来自内部的onRequestPermissionsResult处理结果的结果,因为Google文档说here

<强>更新

您提到的权限是分组示例

Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE

它只会获得一个权限,一个将用于呼叫(阅读电话状态),并且您不需要请求访问网络状态和互联网的权限