即使关闭了应用程序,如何每天在特定时间显示通知? [英] How to show a notification everyday at a certain time even when the app is closed?

查看:84
本文介绍了即使关闭了应用程序,如何每天在特定时间显示通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管可能在Stack Overflow上曾问过这个问题,但我仍然没有找到明确的答案.

Although this question might have been asked before on Stack Overflow, I still haven't found a clear answer.

例如,即使应用已关闭,我也想每天在晚上12点显示一条通知.我从这些链接中引用了以下内容:每天特定时间的android通知在特定时间的Android每日重复通知每天使用AlarmManager Android BroadcastReceiver在启动时进行-当Activity在后台运行时继续运行还有更多...我对Service和BroadcastReceiver之间的区别感到困惑.我应该使用哪一个?还是我都应该使用它们?

I want to show a notification everyday at 12pm for example even when the app is closed. I have referenced from those links: Notifications in specific time every day android, Android daily repeating notification at specific time of a day using AlarmManager, Android BroadcastReceiver on startup - keep running when Activity is in Background and much more... I'm confused on the difference between Service and BroadcastReceiver. Which one shall I use? or shall I use both of them?

到目前为止,我知道如何显示通知,但是我不知道如何即使在关闭应用程序的情况下每天自动显示一次通知.

So far, I know how to show a notification, but I don't know how to show it automatically once everyday even when the app is closed.

我的代码:

public class NotifyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Service created", Toast.LENGTH_LONG).show();

        Intent resultIntent = new Intent(this, HomeScreen.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);

        Notification.Builder notification = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("App Title")
                .setContentText("Some Text...")
                .setContentIntent(resultPendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT < 16) {
            notificationManager.notify(1, notification.getNotification());
        } else {
            notificationManager.notify(1, notification.build());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service destroyed", Toast.LENGTH_LONG).show();
    }
}

AppManifest.xml:

<service android:name=".NotifyService" />

我应该如何编写代码以完成所需的工作?有什么建议或我可以理解的良好链接吗?

How should I write my code to accomplish what I want? Any suggestions or any good link that I can understand from?

推荐答案

如果我对您的理解正确,我相信您需要使用 AlarmManager 设置定期警报.您还需要在设备重启时设置启动警报服务.您可以编写执行所需操作的方法,以便在警报运行时执行该方法,例如显示通知.以下链接应为您提供帮助:

If I understood you correctly, I believe that you need setup a recurring alarm using AlarmManager. You also need to setup starting alarm service on device reboot. You can write a method that does what you want so it get executed when the alarm runs e.g. show notification. The following links should help you:

这篇关于即使关闭了应用程序,如何每天在特定时间显示通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆