簡體   English   中英

如何在特定時間后在Android中顯示通知

[英]How to show notification after certain time in Android

我正在開發一個基於某些通知的應用程序,通過該應用程序用戶可以執行我想顯示通知的操作,並在通知之后我想添加添加時間+ 5分鍾以顯示下一個通知。 並在應用關閉時在后台運行計時器進程。

 public long Time = 3600;
 private NotificationManagerCompat notificationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        notificationManager = NotificationManagerCompat.from(this);
        Timer();

    }


    public void Timer() {

        for (long i = Time; i < 86400000; i = i += 60000) {

            new CountDownTimer(1000, i) {
                @Override
                public void onTick(long millisUntilFinished) {

                }

                @Override
                public void onFinish() {
                    ShowNotification();


                }
            }.start();
        }


    }

    public void ShowNotification() {
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
                .setContentTitle("Example")
                .setContentText("Example Action")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_REMINDER)
                .build();

        notificationManager.notify(1, notification);


    }

在這種情況下,請嘗試使用AlarmReceiver。

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.notify(0, notification);
                    AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.HOUR, 2);
                    Intent intent = new Intent("android.action.DISPLAY_NOTIFICATION");
                    intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                    PendingIntent broadcast = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM