gpt4 book ai didi

未授予 Android 权限

转载 作者:行者123 更新时间:2023-11-29 01:26:06 27 4
gpt4 key购买 nike

我有一个小型测试应用程序,它会尝试显示通话记录。

我测试过测试应用程序的物理手机是: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>

主 Activity .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 和 READ CONTACTS 是否被授予的逻辑,但我总是出错。

为什么声明的权限没有被授予以及如何强制授予它们???

谢谢

最佳答案

根据文档:

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

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

如果应用有权限,该方法返回PackageManager.PERMISSION_GRANTED,应用可以继续操作。如果应用程序没有权限,该方法返回 PERMISSION_DENIED,并且应用程序必须明确询问用户权限。

关于未授予 Android 权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33978401/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com