我开发了一个应用程序,启动一个自定义服务,启动一个倒计时器。该服务在计时器的开始和结束时的其他时间运行通知。 在测试过程中,我看到有关通知和锁定屏幕的问题。我描述了我所做的测试:安卓锁定设备通知不听起来
1)如果我运行该应用程序,并且不让手机关闭或锁定,则该服务将正确播放通知声音,就好像主应用程序(活动)在前面一样。在这两种情况下,服务都会完美地发出通知。
2)如果我锁定了手机,并且应用程序在锁定之前位于前面,则该服务会正确播放通知声音。
3)这里是我的问题:如果我锁定手机,并且在锁定之前应用程序没有在前面,通知不会响起,但服务看起来像正在运行,因为当我按下“on”按钮时我的手机,然后在屏幕打开之后连续播放,它会播放通知(声音和图标),只需在锁定屏幕中解锁手机即可。
我的服务代码将取消先前的通知,这是定时器运行后通知代码结束:
private void notificacion_fin() {
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(0);
Notification not = new Notification();
// Ponemos la nueva notificacion de que hemos acabado
SharedPreferences preferences = this.getSharedPreferences("com.fsp.mypref", 0);
String not_sound = preferences.getString("notification_tone","");
not.audioStreamType = AudioManager.STREAM_NOTIFICATION;
not.sound = Uri.parse(not_sound);
if (tipo_cronometro == 1) {
not.icon = R.drawable.ic_stat_notify_time;
not.tickerText = "Task has finished";
} else {
not.icon = R.drawable.ic_stat_notify_complete;
not.tickerText = "Type 2 task has finished";
}
not.when = System.currentTimeMillis();
not.defaults |= Notification.DEFAULT_VIBRATE;
not.vibrate = new long[] {300,300,300,300,300};
not.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
not.setLatestEventInfo(this, "My app", not.tickerText, contentIntent);
notificationManager.notify(0, not);
notificacion = true;
}
我怀疑我的问题可能是一些相对而言, 会这样?有谁知道会发生什么? 为什么我的第3次测试不能正确运行?
任何帮助将是很好的。
在此先感谢。
P.S:当我说通知声音我的意思是声音+图标
可以使用desrcibe如何解决您的问题我也面临同样的问题...你可以发表你的正确的代码 – user3233280