1

I have set up AlarmManager for 1 minute for test mode. It sends broadcast to OnAlarmReceiver. OnAlarmReceiver start sendWakefulWork(context, TaskService); and in TaskService in doWakefulWork() method i sendOrderedBroadcast which fire Notification. This works well^ notification with sound and vibration appear. But only when phone in active mode. When phone in sleep mode (screen is switched off) only vibration and flash work during notification. No sounds from notification in sleep mode, only vibration. Sometimes it make sounds, but only sometimes , for example make 1 sound in hour, but it should make sound every minute. Also it make sounds every minute in sleep mode when phone connected to the computer but when I unplug phone from computer it starts only vibrate. When I turn on screen everything becomes normal: notification plays sound + vibration.

So I have no idea if it problem with my code or with my phone (HTC Desire Android 2.2)

I have tried a lot of things to fix it:

  • default and custom sounds
  • notification.audioStreamType = AudioManager.STREAM_SYSTEM and others
  • with flash and without flash
  • with vibration and without vibration

Nothing. Only sounds disappear in sleep mode, vibration worked perfect.

public class OnBootReceiver extends BroadcastReceiver {

public static void setAlarm(Context ctxt) {

           AlarmManager mgr = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
   mgr.setRepeating(AlarmManager.RTC_WAKEUP,
           System.currentTimeMillis()+1000,
           1*60*1000,
           getPendingIntent(ctxt));
}

public static void cancelAlarm(Context ctxt) {
   AlarmManager mgr = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
   mgr.cancel(getPendingIntent(ctxt));
}

private static PendingIntent getPendingIntent(Context ctxt) {
   Intent i=new Intent(ctxt, OnAlarmReceiver.class);
   return PendingIntent.getBroadcast(ctxt, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
}

@Override
public void onReceive(Context ctxt, Intent intent) {
   setAlarm(ctxt);
}
}

My doWakefulWork method in TaskService class:

protected void doWakefulWork(Intent intent) {
Intent mIntent = new Intent(EXERCISE_EVENT);
   mIntent.putExtra(StringUtils.PARAM_NEXT_TASK_TIME, nextStringTime);
   mIntent.putExtra(StringUtils.PARAM_TASK_NUMBER, taskNumber);

   sendOrderedBroadcast(mIntent, null);
}

And this is my NotificationBrodcastReceiver :

public class NotificationBrodcastReceiver extends android.content.BroadcastReceiver {

public void onReceive(Context ctx, Intent intent) {

           String ns = Context.NOTIFICATION_SERVICE;
           NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(ns);

           int icon = R.drawable.notif_icon;
           CharSequence tickerText = ctx.getResources().getString(R.string.notification_ticker);
           long when = System.currentTimeMillis();

           Notification notification = new Notification(icon, tickerText, when);
           notification.flags |= Notification.FLAG_AUTO_CANCEL;

           notification.defaults |= Notification.DEFAULT_SOUND;
           notification.defaults |= Notification.DEFAULT_VIBRATE;

           Intent notificationIntent = new Intent(ctx, MainActivity.class);
           PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
           notification.setLatestEventInfo(ctx, "contentTitle", "contentText", contentIntent);
           mNotificationManager.notify(NOTIFICATION_ID, notification);
}
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
higsBozone
  • 302
  • 3
  • 10
  • What is your evidence that this has anything to do with `WakefulIntentService`? – CommonsWare Jul 13 '12 at 23:43
  • I don't know exactly. But when I added this code to the onReceive method in NotificationBrodcastReceiver the sound appear in sleep (I think this is wrong and ugly workaround) :`PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Tag"); wl.acquire(2000);` – higsBozone Jul 14 '12 at 07:50

1 Answers1

2

You are using an ordered broadcast to raise your Notification. Presumably, you are doing this because you want an activity to handle the request if the activity is in the foreground and has a higher-priority BroadcastReceiver for this broadcast.

That pattern is fine for non-wakeful situations. However, once doWakefulWork() ends, nothing is going to keep the device awake, and so the device will fall back asleep. Your ordered Broadcast happens asynchronously, so the its work will happen after doWakefulWork() ends, and so your Notification may not happen at all. If the Notification is partially happening, as you describe, I would personally consider this behavior to be a bug in Android (it should hold its own WakeLock for the purposes of playing back the ringtone), but it is entirely possible that your BroadcastReceiver simply does not get control at all, because the device falls asleep before the broadcast is delivered.

Your hold-a-WakeLock-for-two-seconds workaround will increase the odds of success, but that too is not guaranteed. The only guaranteed ways to ensure that you will get a chance to raise the Notification is to either:

  1. do that work inside of doWakefulWork(), or

  2. hold a WakeLock until the broadcast is completed and the Notification is raised.

I will look to add additional features to the WakefulIntentService framework in the future to handle your scenario better, as what you appear to want to do is reasonable, and it would be nice to have a common implementation of the pattern. However, what you are trying to do is definitely outside the current scope of what WakefulIntentService does.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • See https://github.com/commonsguy/cwac-wakeful/issues/9 for the issue where I am tracking this feature request. – CommonsWare Jul 14 '12 at 10:19
  • i have tried your WakefulIntentService framework but when phone is lock and in sleep that time alarm is not triggered once i unlock it that time immediately it is firing.how to fired even phone is locked? whats your opinion on that one – Ghouse Apr 25 '14 at 06:58
  • @Ghouse: Perhaps you did not set the alarm properly. Perhaps you are running on a device that disables alarms in certain power situations. You are welcome to click the "Ask Question" and explain your problems in much greater detail. – CommonsWare Apr 25 '14 at 11:34
  • Thanks for the advice it was problem with my device when i tried it in another device it worked properly – Ghouse Apr 25 '14 at 12:45