运行时权限对话框未显示Android 7.1

时间:2017-06-29 10:46:35

标签: android android-permissions

我需要初始化服务,它需要READ_PHONE_STATE的权限。我声明并动态应用。回调函数已被重写。但是,虚拟机不会弹出一个请求对话框允许。

代码如下:

的AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

动态应用:

requestPermissions(new String[]{Manifest.permission.CALL_PHONE},1);

请帮帮我。深表感激。

3 个答案:

答案 0 :(得分:0)

使用以下函数检查onCreate()方法中的运行时权限:

   checkPermission();
   requestPermission();

和功能是:

 private boolean checkPermission() {
    return ( ContextCompat.checkSelfPermission(getApplicationContext(), READ_PHONE_STATE ) == PackageManager.PERMISSION_GRANTED);
}

private void requestPermission() {
    ActivityCompat.requestPermissions(LoginActivity.this, new String[]{READ_PHONE_STATE}, REQUEST_SMS);
}

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_SMS:
            if (grantResults.length > 0 &&  grantResults[0] == PackageManager.PERMISSION_GRANTED){
            }else {
                Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access and sms", Toast.LENGTH_SHORT).show();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (shouldShowRequestPermissionRationale(READ_PHONE_STATE)) {
                        showMessageOKCancel("You need to allow access to both the permissions",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                            requestPermissions(new String[]{READ_PHONE_STATE},
                                                    REQUEST_SMS);
                                        }
                                    }
                                });
                        return;
                    }
                }
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

答案 1 :(得分:0)

&#13;
&#13;
import static android.Manifest.permission.SEND_SMS;


public class CoreActivity extends AppCompatActivity implements OnAccountListener {

    boolean init_state;
    AccountApi accountApi;
    UiHandler uiHandle=new UiHandler(this);


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Message msg=new Message();
        msg.what=MsgCode.STATE_LOGIN_START;
        uiHandle.sendMessage(msg);
        ActionBar actionBar=getSupportActionBar();
        actionBar.hide();
        requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},1);
//        PackageManager pm = getPackageManager();
//        boolean permission = (PackageManager.PERMISSION_GRANTED ==
//                pm.checkPermission("android.permission.ACCESS_NETWORK_STATE", "com.example.asus.tl"));
//        if (permission) {
//            Log.d("permission","yes");
//        }else {
//            Log.d("permission","no");
//        }

        checkPermission();
        requestPermission();

        init_state=API.init(this);
        Log.d("API.init",init_state+"");
        Log.d("service",isServiceRunning()+"");

        Thread getAccountThread=new Thread(delayInitApi);
        getAccountThread.start();
    }





    private boolean isServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if ("com.algebra.sdk.Controller".equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    private boolean checkPermission() {
        return ( this.checkSelfPermission(getApplicationContext(), SEND_SMS ) == PackageManager.PERMISSION_GRANTED);
    }

    private int checkSelfPermission(Context applicationContext, String sendSms) {
    return 1;
    }

    private void requestPermission() {
        ActivityCompat.requestPermissions(CoreActivity.this, new String[]{SEND_SMS}, 1);
    }

    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 &&  grantResults[0] == PackageManager.PERMISSION_GRANTED){
                }else {
                    Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access and sms", Toast.LENGTH_SHORT).show();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (shouldShowRequestPermissionRationale(SEND_SMS)) {
//                            showMessageOKCancel("You need to allow access to both the permissions",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                                requestPermissions(new String[]{SEND_SMS},
                                                        1);
                                            }
                                        }
                                    };
                            return;
                        }
                    }
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
&#13;
&#13;
&#13;

答案 2 :(得分:0)

你应该使用

 <uses-permission-sdk-23 android:name="android.permission.READ_PHONE_STATE" />

而不是

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

在你的清单中