我知道,之前曾多次询问过这个问题,但我仍然没有让这个问题起作用,因为我不明白究竟要做什么,或者给定的答案对我不起作用.. 无论如何,我想在日历中添加一个事件...因此,我将permission.WRITE_CALENDAR添加到我的清单中,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.neuertestdiesenbenutzen">
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我的MainActivity看起来像这样:
package com.example.user.neuertestdiesenbenutzen;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CalManager c = new CalManager(getApplicationContext());
int permissionCheck =
ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_CALENDAR);
if(permissionCheck== PackageManager.PERMISSION_GRANTED) {
Log.d("mychecks", "Permission granted");
c.addEvToCal();
}
else{
Log.d("mychecks", "Permission denied");
}
}
}
但是,它无法识别权限,我得到输出:权限被拒绝。
任何人都能以可理解的方式描述在这种情况下该怎么做吗?
感谢您的帮助!
答案 0 :(得分:0)
您需要获得高于6.0版本的运行时权限
尝试此操作以获得运行时权限:
public static final String[] NECESSARY_PERMISSIONS = new String[] {
Manifest.permission.WRITE_CALENDAR,
};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(PasswordSetupActivity.this,
Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ActivityCompat.requestPermissions(
(Activity) PasswordSetupActivity.this,
NECESSARY_PERMISSIONS, 123);
}
}, 1400);
}
}
如果有任何问题可以随意问我
答案 1 :(得分:0)
确保您的项目 targetSdkVersion
为23或更高
试试这个
请求这样的许可
ActivityCompat.requestPermissions(Activity.this,
new String[]{Manifest.permission.WRITE_CALENDAR},
1);// pass here permission request code to handle permission result
处理权限导致 onRequestPermissionsResult
知道用户是否允许权限
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
c.addEvToCal();
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
}
}