checkSelfPermission无法解决

时间:2019-01-21 16:05:15

标签: android

我正在考虑在Android中询问自我(动态权限)的活动上下文。我收到错误checkSelfPermission can not be resolved


 package com.example.asus.provider;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class test  extends Activity {

    if (ContextCompat.checkSelfPermission(test.this , Manifest.permission.READ_CONTACTS )!= PackageManager.PERMISSION_GRANTED){

        /// ToDo ....

     }
}

3 个答案:

答案 0 :(得分:1)

添加后问题已解决

公共无效onCreate(捆绑保存的InstanceState){         super.onCreate(savedInstanceState);

public class  test  extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if (ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CONTACTS )!= PackageManager.PERMISSION_GRANTED)  {

            /// ToDo ....
            Toast.makeText(test.this, "Permission (not) Granted!", Toast.LENGTH_SHORT).show();
        }
    }
}

答案 1 :(得分:0)

尝试扩展JuryMember而不是AppCompatActivity

编辑:将Activity替换为ContextCompat

这是我使用的:

ActivityCompat

答案 2 :(得分:0)

在这里,您需要在各种情况下拨打电话,

如果有活动:

df[df.LNAME.replace(regex=r'(?i)'+ df.FNAME,value=True)==True]
  FNAME    LNAME
1  Tobi  Tobiwan

如果是片段:

ContextCompat.checkSelfPermission(MyActivity.this,
        Manifest.permission.READ_CONTACTS);

对于任何实用程序类,请使用上下文:

ContextCompat.checkSelfPermission(getActivity(),
        Manifest.permission.READ_CONTACTS);

有关更多信息,您可以参考https://developer.android.com/training/permissions/requesting