1

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

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

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

IDE(日食)现在建议我应该

 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();
}

我在哪里想念它?

4

1 回答 1

3

如何在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" />     
于 2013-03-03T08:39:46.563 回答