android - 如何在特定时间在android上发出通知?

标签 android android-notifications

我想在特定时间向我的应用发出通知。说每天早上 7 点我必须通知,即使应用程序已关闭。

我该怎么做?有教程吗? 请注明链接。

最佳答案

首先您需要使用广播接收器。并且因为广播接收器只启动了很短的时间

from the android developer blog.When handling a broadcast, the application is given a fixed set of time (currently 10 seconds) in which to do its work. If it doesn't complete in that time, the application is considered to be misbehaving, and its process immediately tossed into the background state to be killed for memory if needed.

在此处使用 Intent 服务是一种更好的做法,您有一个示例。

这是广播接收器类。

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        Intent intent1 = new Intent(context, MyNewIntentService.class);
        context.startService(intent1);
    }
}

并在 list 中注册。

<receiver
    android:name=".MyReceiver"
    android:enabled="true"
    android:exported="false" >
</receiver>

这是 Intent 服务类。

public class MyNewIntentService extends IntentService {
    private static final int NOTIFICATION_ID = 3;

    public MyNewIntentService() {
        super("MyNewIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Notification.Builder builder = new Notification.Builder(this);
            builder.setContentTitle("My Title");
            builder.setContentText("This is the Body");
            builder.setSmallIcon(R.drawable.whatever);
        Intent notifyIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        //to be able to launch your activity from the notification 
        builder.setContentIntent(pendingIntent);
        Notification notificationCompat = builder.build();
        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
        managerCompat.notify(NOTIFICATION_ID, notificationCompat);
    }
}

并在 list 中注册。

<service
    android:name=".MyNewIntentService"
    android:exported="false" >
</service>

然后在您的 Activity 中设置警报管理器以在特定时间启动广播接收器并使用 AlarmManager setRepeating 方法重复它下面的示例将每天重复。

 Intent notifyIntent = new Intent(this,MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast
            (context, NOTIFICATION_REMINDER_NIGHT, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,  System.currentTimeMillis(),
            1000 * 60 * 60 * 24, pendingIntent);

希望对你有帮助。

关于android - 如何在特定时间在android上发出通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34517520/

相关文章:

android - 如何创建一个填充父级高度并显示尽可能大的 Image 的 ImageView?

javascript - 从 android 和 IOS 直接 javascript 打印

java - 如何在Android中获取多个通知

android - 从通知中实现 setOnClickPendingIntent

java - 使用 Robospice 在 Android 上固定证书

android - 无效的用户数据异常 - Android

android - 在 Android 的谷歌地图中添加了多少个最大标记?

java - setAutoCancel() 在 API 26 中不起作用

java - 将通知点击数据发送到子 Activity

android - 我想从我的 PHP 应用程序向我的 android 应用程序发送通知