我正在开发一个原生的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)
任何线索?
答案 0 :(得分:5)
Android权限区分大小写。请改用:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
答案 1 :(得分:4)
尝试将权限更改为
<uses-permission android:name="android.permission.WAKE_LOCK" />
我认为大写字母在这里有所不同,但我可能错了。
参考:https://developer.android.com/training/scheduling/wakelock.html