我已经为测试模式设置了 1 分钟的 AlarmManager。它向 OnAlarmReceiver 发送广播。OnAlarmReceiver 启动 sendWakefulWork(context, TaskService); 在 doWakefulWork() 方法中的 TaskService 中,我发送了触发通知的OrderedBroadcast。这很好用^出现带有声音和振动的通知。但仅当手机处于活动模式时。当手机处于睡眠模式(屏幕关闭)时,通知期间只有振动和闪光工作。睡眠模式下没有通知声音,只有振动。有时它会发出声音,但只是有时会发出声音,例如每小时发出 1 次声音,但它应该每分钟发出声音。此外,当手机连接到计算机时,它在睡眠模式下每分钟都会发出声音,但是当我从计算机上拔下手机时,它只会开始振动。当我打开屏幕时,一切都变得正常:通知播放声音+振动。
所以我不知道我的代码或手机是否有问题(HTC Desire Android 2.2)
我已经尝试了很多方法来修复它:
- 默认和自定义声音
- notification.audioStreamType = AudioManager.STREAM_SYSTEM 等
- 带闪光灯和不带闪光灯
- 有振动和无振动
没有。只有声音在睡眠模式下消失,振动完美。
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);
}
}
我在 TaskService 类中的 doWakefulWork 方法:
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);
}
这是我的 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);
}
}