Tib*_*bor 5 service notifications android locking
我开发了一个应用程序,它启动一个触发倒计时的自定义服务。该服务在计时器开始时运行通知,并在结束时运行其他通知。在测试期间,我发现有关通知和锁定屏幕的问题。我描述了我所做的测试:
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;
}
Run Code Online (Sandbox Code Playgroud)
我怀疑我的问题可能与上下文有关,可能是?有谁知道会发生什么?为什么在我的第三次测试中没有正确运行?
任何帮助都会受到欢迎。
提前致谢。
PS:当我说通知声音时,我的意思是声音+图标
好吧,只要您锁定屏幕(通常在一两分钟内),手机就会进入睡眠模式。您有三个选择,其中两个更好。
startForeground
您的服务。这样,它基本上就会位于前面。但它仍然会受到睡眠模式的影响。BroadcastReceiver
通知发出声音。如果您需要一个可视倒数计时器,那么也可以运行该服务,但不要依赖服务在没有 WAKE_LOCK 的情况下保持唤醒状态。如果用户的手机不好或尝试使用使用过多 RAM 的应用程序,服务也可能会被破坏 - 我的手机可能会杀死它,因为我的手机很糟糕。
我喜欢唤醒锁,但警报管理器可能是最好的方法。
Context
无论手机处于唤醒还是睡眠状态,它都会保持不变。上下文基于它所在的正在运行的进程/类/代码块。如果上下文导致问题,我希望它抛出异常并强制关闭。
归档时间: |
|
查看次数: |
2695 次 |
最近记录: |