【问题标题】:Notifications coming immediately after App being destroyedApp被销毁后立即通知
【发布时间】:2014-07-26 09:31:26
【问题描述】:

我已经设置了一个通知,该通知将在应用程序被销毁 4 小时后显示。

但是当我销毁应用程序时,通知会立即发出,也会在 4 小时后发出。

这是我的 Main Activity 的 onDestroy():

@Override
    public void onDestroy() {
        super.onDestroy();


        Intent myIntent = new Intent(this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        //alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 3600*1000*4, 3600*1000*4, pendingIntent);
    }

这是我的 AlarmReceiver.class:

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent mainIntent = new Intent(context, Main.class);

        NotificationManager notificationManager
            = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        android.app.Notification noti = new NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setContentIntent(PendingIntent.getActivity(context, 0, mainIntent,
                              PendingIntent.FLAG_UPDATE_CURRENT))
            .setContentTitle("Title")
            .setContentText("It's been so Long!!!")
            .setSubText("Please return back to App & Learn more Duas.")
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setVibrate(new long[] {700,700,700,700})
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker("Important Notification")
            .setWhen(System.currentTimeMillis())
            .build();

        notificationManager.notify(0, noti);
    }

}

我希望每 4 小时后显示通知,而不是在应用被销毁后立即显示。

任何帮助都将受到高度赞赏。

提前致谢。

【问题讨论】:

    标签: android notifications broadcastreceiver alarmmanager repeatingalarm


    【解决方案1】:

    因为小错误

    只需更改这一行

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 3600*1000*4, 3600*1000*4, pendingIntent);
    

    到这里

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis()+(3600*1000*4)), 3600*1000*4, pendingIntent);
    

    【讨论】:

    猜你喜欢
    • 2022-11-13
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 2014-04-02
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多