如何延迟程序,直到用户响应运行时权限?

时间:2017-06-07 14:30:01

标签: android

我的运行时权限有问题。我的代码是:

     public class MainActivity extends AppCompatActivity {
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
    ActivityCompat.requestPermissions(MainActivity.this, permissions, 0x3);
    while (i == 0) {
        //
    }
    //
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 0x3:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
              Toast.makeText(G.context, "allowed",Toast.LENGTH_LONG).show();
            } else {
               Toast.makeText(G.context, "deney", Toast.LENGTH_LONG).show();
            }
            break; } }}

我希望在用户响应权限之后运行阻止块。但是块不要等待许可结果。如何延迟程序,直到用户对运行时权限的响应,以及运行whlie和另一行之后?

1 个答案:

答案 0 :(得分:0)

如果"允许&#34>您只需要调用其余的代码。你的onRequestPermissionResult方法的案例。

if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
      Toast.makeText(G.context, "allowed",Toast.LENGTH_LONG).show();
      //The rest of your code
} else {
      Toast.makeText(G.context, "deney", Toast.LENGTH_LONG).show();
      //Ask again
}