0
我一直在爲此奮鬥了很長一段時間,我正在使用鬧鐘管理器安排鬧鐘,我的清單文件中有一個接收器聲明。警報和接收器在應用程序運行時或在後臺運行時按預期工作,在用戶關閉應用程序後,無法啓動鬧鐘。我基本上只是試圖在我的應用程序中有本地通知。這裏沒有其他的「答案」有很大的幫助。應用程序關閉後是否有可能發出本地通知?Android Studios鬧鐘管理器應用程序已關閉
public static void writtenGoalsNotification(Context context) {
final int _id = 15;
pref = context.getSharedPreferences("userpref", 0);
Notification notification = getNotification("Don't forget to write your daily goals!", context, "none", "Goals");
Intent notificationIntent = new Intent(context, NotificationReceiver.class);
notificationIntent.putExtra(NotificationReceiver.NOTIFICATION_ID, "written goals notification");
notificationIntent.putExtra(NotificationReceiver.NOTIFICATION, notification);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, _id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Integer hour = pref.getInt(NotificationKeys.Goals.ReminderTime.hour, 10);
Integer minute = pref.getInt(NotificationKeys.Goals.ReminderTime.minute, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.MILLISECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent);
}
這裏是我的接收器,
public class NotificationReceiver extends WakefulBroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
startWakefulService(context, intent);
WakeLocker.acquire(context);
WakeLocker.release();
}
}
這是我的清單,
<receiver
android:name=".Controllers.NotificationReceiver"
</receiver>
顯示錯誤日誌 – DroiDev
我能得到的東西更多的描述?代碼不會產生任何錯誤,通知在應用程序運行或後臺運行時正確發生。問題在於,一旦應用程序關閉,警報或接收器就會被銷燬。 –
我誤解了這個問題。接受我的道歉。我認爲你需要擴展一個boardcast接收器....我從來沒有與清醒的廣播接收器工作。希望別人可以幫助你解決問題。 – DroiDev