我已经制作了一个安卓应用,并使用AlarmManager和Broadcast Receiver来获取本地通知。但我的Receiver类根本没有被调用。我追踪了问题并发现我的应用无法获得“SET_ALARM”权限。请查看下面的屏幕截图。
屏幕截图:
清单文件
为了检查权限问题,我在MainActivity.java中添加了以下代码,以检查应用程序是否能够获取权限。我发现它无法获得请求的SET_ALARM权限。请查看下面的代码。
MainActivity.java
package com.dileepmanuballa224.alarm_test;
import android.Manifest;
import android.app.AlarmManager;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
AlarmManager am;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(ContextCompat.checkSelfPermission(this, Manifest.permission.SET_ALARM)!= PackageManager.PERMISSION_GRANTED){
Log.d("Perm check:SET_ALARM", "Permission Denied");
requestPermissions(new String[]{Manifest.permission.SET_ALARM},1);
}else{
Log.d("Perm check:SET_ALARM", "Permission Exists");
}
if(ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET)!= PackageManager.PERMISSION_GRANTED){
Log.d("Perm check:INTERNET", "Permission Denied");
requestPermissions(new String[]{Manifest.permission.SET_ALARM},1);
}else{
Log.d("Perm check:INTERNET", "Permission Exists");
}
}
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dileepmanuballa224.alarm_test">
<uses-permission android:name="android.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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>
<receiver android:name=".AlarmReceiver"/>
</application>
</manifest>
日志结果:
有人能帮我解决这个问题吗。