views:

155

answers:

1

I'm trying to generate a notification which vibrates the phone and plays a sound when the screen is off (cpu turned off). According to the Log messages, the notification is being sent, but the phone doesn't vibrate or play the sound until I turn the screen on again. I tried holding a 2 second temporary wakelock (PowerManager.PARTIAL_WAKE_LOCK), which I thought would be ample time for the notification to be played, but alas, it still doesn't.

Any pointers to get the notification to run reliably? I'm testing this on an G1 running Android 1.6.

Code I'm using:

notif.vibrate = new long[] {100, 1000};
notif.defaults |= Notification.DEFAULT_SOUND;

notif.ledARGB = Color.RED;
notif.ledOnMS = 1;
notif.ledOffMS = 0;
notif.flags = Notification.FLAG_SHOW_LIGHTS;

notif.flags |= NOTIF_FLAGS; //static var

if (!screenOn) { //var which updates when screen turns off/on
    mWakeLock.acquire(2000); 
}

manager.notify(NOTIF_ID, notif);
A: 

You can make your own receiver that extends to BroadcastReceiver which can perform your notification when it receives INTENT.ACTION_SCREEN_OFF

@Override 
public void onReceive(Context context, Intent intent) {
  Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
  long milliseconds = 1000;  
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        // vibrate the phone
            v.vibrate(milliseconds);
        // any other code here            
     }   
}
Anthony Forloney
The notification doesn't need to be shown when the screen turns off, it shows it when the socket disconnects.
Al
I removed the notification comment, but try to see if the above code works for you. Since I do not know how your code looks, this could be either helpful or redundant
Anthony Forloney
The notification already has the vibrate option set into it using notification.vibrate = new long[] {100, 1000}. The notification could be shown at any time while my app is running, the problem is the notification doesn't play when the screen is off. I don't need to do anything with the notification when the screen off broadcast is sent.
Al
I was just using the above code to make sure you can receive the intent and then use vibrate, but can you post the code in question so I can see whats going on?
Anthony Forloney
Code added. I receive a call to my method when the socket disconnects and this in turn leads to the code I posted.
Al