简体   繁体   English

未授予 Android 权限

[英]Android permissions are not being granted

I have a small test app, that attempts to display the call log.我有一个小的测试应用程序,它试图显示通话记录。

Physical phones that I have tested the test app are : LG G2 Android 4.4.2 Kernel 3.4.0 And Samsung S5 Android 4.4.2 Kernel 3.4.0.我已经测试过测试应用程序的物理手机是:LG G2 Android 4.4.2 Kernel 3.4.0 和 Samsung S5 Android 4.4.2 Kernel 3.4.0。

I have given the app these permissions:我已授予应用程序以下权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="acl.test.com.acl" >

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.WRITE_CALL_LOG" />
    <uses-permission android:name="android.permission.Calls.CONTENT_URI" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name="acl.test.com.acl.aclApp">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

MainActivity.java主活动.java

public class MainActivity extends AppCompatActivity {

    public static String appTagForLog = "ACL";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();

        boolean hasREAD_CALL_LOG = SecurityUtil.doesUserHavePermission(SecurityUtil.READ_CALL_LOG);
        boolean hasCONTENT_URI = SecurityUtil.doesUserHavePermission(SecurityUtil.CONTENT_URI);
        boolean hasREAD_CONTACTS = SecurityUtil.doesUserHavePermission(SecurityUtil.READ_CONTACTS);
        try {


            //check security
            if (hasREAD_CALL_LOG && hasCONTENT_URI && hasREAD_CONTACTS) {
                Log.d(appTagForLog, "app has all permissions");
                StringBuffer sb = CallLogService.getCallLog();
                Log.d(appTagForLog, sb.toString());

            } else {
                //kind of error, app will not be useful alert user
                String messageToUser = "The user must enable ";
                if (hasREAD_CALL_LOG) {
                    messageToUser = messageToUser + "READ CALL LOG";
                }
                if (hasCONTENT_URI) {
                    messageToUser = messageToUser + ", CALL DATA";
                }
                if (hasREAD_CONTACTS) {
                    messageToUser = messageToUser + ", READ_CONTACTS";
                }

                messageToUser = messageToUser + " permissions!";

                Toast.makeText(
                        this,
                        messageToUser,
                        Toast.LENGTH_LONG).show();

                Log.d(appTagForLog, messageToUser);

                StringBuffer sb = CallLogService.getCallLog();
                Log.d(appTagForLog, "***CALL LOG:"+ sb.toString());

            }
        }
        catch (Exception exc)
        {
            exc.printStackTrace();
            Log.d(appTagForLog, "Exception, message: "+exc.getMessage());
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}//end of main activity 

I have added logic in the MainActivity that checks whether READ_CALL_LOG & READ CONTACTS was granted, however I keep getting false.我在 MainActivity 中添加了检查 READ_CALL_LOG 和 READ CONTACTS 是否被授予的逻辑,但是我一直在出错。

Why are declared permissions not being granted & how can they be forced to be granted???为什么未授予声明的权限以及如何强制授予权限???

Thanks谢谢

According to the docs:根据文档:

To check if you have a permission, call the ContextCompat.checkSelfPermission() method.要检查您是否有权限,请调用ContextCompat.checkSelfPermission()方法。 For example, this snippet shows how to check if the activity has permission to write to the calendar:例如,此代码段显示了如何检查 Activity 是否具有写入日历的权限:

// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
    Manifest.permission.WRITE_CALENDAR);

If the app has the permission, the method returns PackageManager.PERMISSION_GRANTED , and the app can proceed with the operation.如果应用有权限,该方法返回PackageManager.PERMISSION_GRANTED ,应用可以继续操作。 If the app does not have the permission, the method returns PERMISSION_DENIED , and the app has to explicitly ask the user for permission.如果应用程序没有权限,则该方法返回PERMISSION_DENIED ,应用程序必须明确要求用户授予权限。

If permission is declared in manifest, this should work for you.如果在清单中声明了许可,这应该对您有用。

  public static boolean checkPermission(String permission, Context mContext) {
    return mContext.getPackageManager().checkPermission(permission,
        mContext.getPackageName()) == PackageManager.PERMISSION_GRANTED;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM