简体   繁体   中英

WakeLock Acquisition Issue In Broadcast Receiver

I developing an application where i need a broadcast receiver to generate notifications on receiving messages from GCM push.

The code i am using refuses to compile, stating that

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

The IDE (eclipse) now suggests i should

 remove argument to match "acquire()"

However when i do that, the next error shown is:

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

The code for the broadcast receiver is :

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

Where am i missing it?

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....
}

Add permission in AndroidManifest.xml

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM