未授予Android权限

时间:2015-11-29 01:25:10

标签: android android-permissions

我有一个小型测试应用,试图显示通话记录。

我测试过的测试应用程序的物理手机有:LG G2 Android 4.4.2内核3.4.0和三星S5 Android 4.4.2内核3.4.0。

我已经为应用程序提供了这些权限:

<?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

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 

我在MainActivity中添加了逻辑,用于检查READ_CALL_LOG&amp; READ CONTACTS被授予,但我一直都是假的。

为什么声明的权限未被授予&amp;他们怎么能被迫被授予?

由于

2 个答案:

答案 0 :(得分:1)

根据文件:

要检查您是否拥有权限,请调用ContextCompat.checkSelfPermission()方法。例如,此代码段显示了如何检查活动是否有权写入日历:

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

如果应用具有权限,则该方法返回PackageManager.PERMISSION_GRANTED,应用可以继续操作。如果应用程序没有权限,则该方法返回PERMISSION_DENIED,应用程序必须明确要求用户许可。

答案 1 :(得分:0)

如果在清单中声明了权限,这应该对您有用。

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