在 Android 1.5 服务中使用唤醒锁
您好,我正在尝试使用服务来控制唤醒锁,以便我可以在应用程序运行时永久保持屏幕打开。 我创建唤醒锁并在 onCreate() 中激活它并在 onDestroy() 中释放它,但是我收到错误“wl 无法解析”。 有人可以解释一下我该如何克服这个问题吗? 代码如下:
public class WakeLockService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire();
}
@Override
public void onDestroy() {
wl.release();
}
}
Hello I am trying to use a service to control a wakelock so I can permanently leave the screen on when my application is running. I create the wakelock and activate it in onCreate() and release it in onDestroy() however I get the error "wl cannot be resolved". Can someone explain how I can get over this? Code below:
public class WakeLockService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire();
}
@Override
public void onDestroy() {
wl.release();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,即使您会使用实例变量,我也认为这不是这样做的方法。
谁会调用
destroy()
? 我希望不是你,这是操作系统的工作。但是,当您持有唤醒锁时,您的 Destroy() 方法不太可能被调用,因为操作系统将首先销毁其他活动/服务。
除此之外,在
onCreate()
方法中获取唤醒锁已经太晚了。 在到达onCreate()
之前,当您通过闹钟或后台活动触发服务时,手机可能已经进入睡眠状态。很难说你应该做些什么不同的事情,因为你没有提供太多背景信息。
通常的事件过程是这样的。
BroadcastReceiver
被调用,并且在onReceive()
中您获取唤醒锁并将其放入服务的静态变量中。 当服务完成时,它应该调用 stopSelf(),释放唤醒锁,然后将保留对该锁的引用的静态变量置空。另外,如果您使用服务,则完全唤醒锁很可能不是您想要的,但部分唤醒锁才是您想要的。 您不需要屏幕保持打开状态,对吧?
抱歉,由于我上面描述的问题,唤醒锁的使用非常复杂。 这绝对是一个高级主题,而且很容易搞砸。 如果你这样做,你的应用程序将得到非常难看的评论,因为坚持太久是一种严重的冒犯,因为它会耗尽电池。
请不要采取错误的方式,但考虑到您在此处发布的问题的性质(语法/编译器错误),我强烈建议搜索没有服务和唤醒锁的解决方案。
Well, even if you would use an instance variable I would think this is not the way to do it.
Who is gonna call
destroy()
? I hope not you, it's the OS job to do so.But when you are holding a wake lock it is highly unlikely that your
destroy()
method get called, because the OS will first destroy other activities/services.Besides that, it's way too late to acquire the wake lock in the
onCreate()
method. BeforeonCreate()
is reached the phone might have gone to sleep already when you trigger the Service from an alarm vs. from an activity that is in the background.It's hard to say what you should make differently as you don't give much context.
The usual course of events is this.
A
BroadcastReceiver
gets called and in theonReceive()
you acquire the wake lock and put it in a static variable on your service. When the service is done it should callstopSelf()
, release the wake lock and then null the static variable that keeps a reference to the lock.Also, if you use a Service a full wake lock is very likely not what you want, but a partial wake lock is. You don't need the screen to stay on, right?
Sorry, but wake locks are really complicated to use, because of exactly the issues I described above. It's definitively an advanced topic and it's easy to screw up. If you do, your app will get very ugly comments, because holding on for too long is a major offense as it drains the battery.
Don't take this the wrong way please, but given the nature of the problem you posted here (syntax / compiler error) I would strongly suggest to search for a solution without a Service and wake lock.
你没有错过
onDestroy() 中的那行吗? 它是 onCreate() 中的局部变量,但在 onDestroy() 中根本没有声明。
或者,更有可能的是,您可能希望将其设为类 WakeLockService 的字段而不是局部变量。
Aren't you missing the line
in onDestroy()? It is a local variable in onCreate(), but it is not declared in onDestroy() at all.
Or, more probable, you may want to make it a field of class WakeLockService instead of a local variable.