我有在每個Chrono.change(目前測試目的)發送批註的應用程序。它的工作很好,當我在MainActivity的功能開發。但是它不適用於任何其他活動或者當按下HOME按鈕時。如果按下POWER按鈕,應用程序將在後臺運行並且工作正常(如果在MainActivity上按POWER按鈕)。當(andorid)主頁按鈕被按下時(背景)如何顯示註釋
任何想法如何解決這個兩個問題:
1)通知還發送,如果在另一個活動
2)更關鍵的IM - 通知是即使按HOME鍵發送和林不目前在應用中。
eventL istener:
stopWatch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
turnedOnOff = prefs.getBoolean("notification",false);
if (turnedOnOff)
throwNotification();
}
});
Nottifications:
public void throwNotification()
{
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, 0);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
.setContentTitle("Finish!")
.setContentText("Its done").setSmallIcon(R.mipmap.notif)
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
noti.defaults |= Notification.DEFAULT_SOUND;
noti.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, noti);
}
感謝暢想!
感謝您的回覆,螺母我不需要重新編程主頁按鈕。我只是需要我的應用程序能夠發送通知,即使主頁按鈕被按下,我目前不在應用程序 –
@jan我的答案包括如何做到這一點。 – GoogleMac