我開發了一個應用程序,啓動一個自定義服務,啓動一個倒計時器。該服務在計時器的開始和結束時的其他時間運行通知。 在測試過程中,我看到有關通知和鎖定屏幕的問題。我描述了我所做的測試:安卓鎖定設備通知不聽起來
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