我开发了一个应用程序,启动一个自定义服务并触发一个倒计时器。该服务在计时器开始和结束时运行通知。在测试过程中,我遇到了关于通知和锁屏的问题。以下是我所做的测试描述:
1)如果我运行应用程序并不让手机关闭或锁定,服务会正确播放通知声音,就好像主应用程序(活动)没有被覆盖一样。在这两种情况下,服务都能完美地触发通知。
2)如果我锁定手机并且在锁定之前应用程序在前台,服务会正确播放通知声音。
3)这就是我的问题:如果我锁定手机并且在锁定之前应用程序不在前台,则通知不会发出声音,但是服务看起来在运行,因为当我按下手机上的“开”按钮时,屏幕立即打开,它会在锁定屏幕上播放通知(声音和图标),就在我解锁手机之前。
我的服务代码取消了先前的通知,以下是在计时器结束后运行通知的代码:
1)如果我运行应用程序并不让手机关闭或锁定,服务会正确播放通知声音,就好像主应用程序(活动)没有被覆盖一样。在这两种情况下,服务都能完美地触发通知。
2)如果我锁定手机并且在锁定之前应用程序在前台,服务会正确播放通知声音。
3)这就是我的问题:如果我锁定手机并且在锁定之前应用程序不在前台,则通知不会发出声音,但是服务看起来在运行,因为当我按下手机上的“开”按钮时,屏幕立即打开,它会在锁定屏幕上播放通知(声音和图标),就在我解锁手机之前。
我的服务代码取消了先前的通知,以下是在计时器结束后运行通知的代码:
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;
}
我怀疑我的问题可能与上下文有关,
可能吗?有人知道发生了什么吗?
为什么我的第三个测试不能正确运行?
欢迎任何帮助。
提前致谢。
P.S.:当我说通知声音时,我的意思是声音+图标。