gpt4 book ai didi

android - 广播接收器中的唤醒锁获取问题

转载 作者:行者123 更新时间:2023-11-29 18:02:28 26 4
gpt4 key购买 nike

我正在开发一个需要广播接收器来生成通知的应用程序从 GCM 推送接收消息。

我正在使用的代码拒绝编译,说明

   "Cannot make a static reference to the non-static method acquire() from
the type PowerManager.Wakelock"

IDE (eclipse) 现在建议我应该

 remove argument to match "acquire()"

但是,当我这样做时,显示的下一个错误是:

 The method acquire(long) in the type PowerManager.WakeLock is not applicable        
for the arguements(Context)....

广播接收器的代码是:

   private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);

WakeLock.acquire(getApplicationContext());



// Showing received message
lblMessage.append(newMessage + "\n");
Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();

// Releasing wake lock
WakeLock.release();
}

我哪里漏了?

最佳答案

How to acquire the wakelock in broadcastReceiver.?

private PowerManager.WakeLock wakeLock;  //Declaration of Instance variable.

@Override
public void onReceive(Context context, Intent intent) {
//......Code.....
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE,"Wake Lock");
wakeLock.acquire(15*1000);
//......Code....
}

在AndroidManifest.xml中添加权限

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

关于android - 广播接收器中的唤醒锁获取问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15183908/

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