本文介绍了无法获得许可:"android.permission.SET_ALARM"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个android应用程序,并使用AlarmManager和Broadcast Receiver来获取本地通知.但是我的Receiver类根本没有被调用.我回溯了该问题,发现我的应用程序无法获得"SET_ALARM"权限.请在下面找到相同的屏幕截图.

I've made an android application and used AlarmManager and Broadcast Receiver to get local notifications. But my Receiver class is not at all being called. I backtraced the issue and found out that my app is not able to get 'SET_ALARM' permission. Please find screenshot below for the same.

屏幕截图:

因此,为了交叉检查权限问题,我在MainActivity.java中添加了以下代码,以检查该应用程序是否能够获取权限.我发现它无法获得请求的SET_ALARM权限.请在下面找到代码.

So to crosscheck the permission issue, I've added the following code in MainActivity.java to check whether the app is able to get permissions or not. I found out that it is not able to get requested SET_ALARM permission. Please find the code below.

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>

日志结果:

任何人都可以帮助我解决这个问题.

Could anyone please help me with this issue.

推荐答案

您在清单中声明的​​权限不是该权限的正确值,该权限的实际值是:

The permission you have declared in your Manifest is not the correct value for the permission, actual value for the permission is this:

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

请参见,不存在这样的权限,即原因是拒绝访问权限,也没有请求访问权限,请将访问权限更改为上述值,一切都会正常进行.

See this, There is no such permission existing that is the reason it is denying the permission as well as not requesting it too, change the permission to the above value and everything will work fine.

此外,如果您想验证,则:

Also if you want to verify that then:

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");
        }

如果您检查Manifest.permission.SET_ALARM的值,您将看到它是"com.android.alarm.permission.SET_ALARM",在这里看到此内容:

If you would inspect the value of Manifest.permission.SET_ALARM, you will see that it will be "com.android.alarm.permission.SET_ALARM", here see this:

这篇关于无法获得许可:"android.permission.SET_ALARM"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 03:17