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