我有在每个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