我已经为我的问题寻找了很多答案,但似乎没有任何效果。我在我的应用程序中使用 ACQUIRE_CAUSES_WAKEUP 唤醒锁。但是每当我在模拟器和设备上运行我的代码时,我都会收到强制关闭错误消息。
我的代码是这样的: -
PowerManager.WakeLock wakeLock;
final PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
检查条件后,调用 startAction() 方法。我需要在这个方法中获取我的唤醒锁。
public void startAction(){
wakeLock.acquire();
alert.setVisibility(View.VISIBLE);
click.setVisibility(View.VISIBLE);
....
}
在停止按钮单击事件中,我释放我的唤醒锁。
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
v.cancel();
tg.stopTone();
tg.release();
shouldRun=false;
myThread.interrupt();
message = (TextView) findViewById(R.id.tvMessage);
message.setVisibility(View.VISIBLE);
message.setText(body);
speakOut();
wakeLock.release();
}
logcat 错误显示:-
02-25 13:05:26.246: E/AndroidRuntime(2026): FATAL EXCEPTION: main
02-25 13:05:26.246: E/AndroidRuntime(2026): java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.accessinbox/com.example.accessinbox.AccessInbox}: java.lang.IllegalArgumentException
02-25 13:05:26.246: E/AndroidRuntime(2026): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-25 13:05:26.246: E/AndroidRuntime(2026): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-25 13:05:26.246: E/AndroidRuntime(2026): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-25 13:05:26.246: E/AndroidRuntime(2026): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-25 13:05:26.246: E/AndroidRuntime(2026): at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 13:05:26.246: E/AndroidRuntime(2026): at android.os.Looper.loop(Looper.java:123)
02-25 13:05:26.246: E/AndroidRuntime(2026): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-25 13:05:26.246: E/AndroidRuntime(2026): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 13:05:26.246: E/AndroidRuntime(2026): at java.lang.reflect.Method.invoke(Method.java:507)
02-25 13:05:26.246: E/AndroidRuntime(2026): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-25 13:05:26.246: E/AndroidRuntime(2026): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-25 13:05:26.246: E/AndroidRuntime(2026): at dalvik.system.NativeStart.main(Native Method)
02-25 13:05:26.246: E/AndroidRuntime(2026): Caused by: java.lang.IllegalArgumentException
02-25 13:05:26.246: E/AndroidRuntime(2026): at android.os.PowerManager$WakeLock.<init>(PowerManager.java:224)
02-25 13:05:26.246: E/AndroidRuntime(2026): at android.os.PowerManager.newWakeLock(PowerManager.java:396)
02-25 13:05:26.246: E/AndroidRuntime(2026): at com.example.accessinbox.AccessInbox.onCreate(AccessInbox.java:63)
02-25 13:05:26.246: E/AndroidRuntime(2026): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-25 13:05:26.246: E/AndroidRuntime(2026): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-25 13:05:26.246: E/AndroidRuntime(2026): ... 11 more