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开发者_如何学C 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);
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
}
}
精彩评论