gpt4 book ai didi

android - 当我将 PowerManager.WakeLock 与 AlaramManager android 一起使用时出现权限异常

转载 作者:太空狗 更新时间:2023-10-29 15:45:51 26 4
gpt4 key购买 nike

我正在开发一个 native android 应用程序,每 30 分钟运行一次操作。我需要确保处理器正在运行,所以我使用了

PowerManager.Wakelock 在警报接收器中。

这是我在内部用于警报接收器的代码。

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {

PowerManager pm = (PowerManager) context.getSystemService(context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
Intent eventService = new Intent(context, SyncInBackground.class);
context.startService(eventService);
wl.release();
}
}

在我的主要 AndroidManifest.xml 中

我添加了以下行来请求权限

<uses-permission android:name="android.permission.wake_lock"/>

当我在我的应用程序中运行警报时,出现以下异常

02-07 23:25:02.839  13699-13699/com.snappext E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start receiver com.snappext.Backup.AlarmReceiver: java.lang.SecurityException: Neither user 10008 nor current process has android.permission.WAKE_LOCK.
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2140)
at android.app.ActivityThread.access$1500(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4512)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.SecurityException: Neither user 10008 nor current process has android.permission.WAKE_LOCK.
at android.os.Parcel.readException(Parcel.java:1327)
at android.os.Parcel.readException(Parcel.java:1281)
at android.os.IPowerManager$Stub$Proxy.acquireWakeLock(IPowerManager.java:381)
at android.os.PowerManager$WakeLock.acquireLocked(PowerManager.java:297)
at android.os.PowerManager$WakeLock.acquire(PowerManager.java:276)
at com.snappext.Backup.AlarmReceiver.onReceive(AlarmReceiver.java:54)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2133)
            at android.app.ActivityThread.access$1500(ActivityThread.java:127)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4512)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
            at dalvik.system.NativeStart.main(Native Method)

有什么线索吗?

最佳答案

Android 权限区分大小写。改用这个:

<uses-permission android:name="android.permission.WAKE_LOCK"/>

关于android - 当我将 PowerManager.WakeLock 与 AlaramManager android 一起使用时出现权限异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28387610/

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