如何在特定时间在android上发出通知? [英] How To give notifications on android on specific time?

查看:211
本文介绍了如何在特定时间在android上发出通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to give notification to my app on a specific time. Say everyday i have to give notification on 7 AM even if the app is closed.

我该怎么做?有教程吗? 请提及链接.

How can i do this? Any tutorial? Please mention the link.

推荐答案

首先,您需要使用广播接收器.而且因为广播接收器只能在短时间内启动

first you need to use a broadcastreceiver. and because a broadcast receiver up only for a short time

android开发者博客中的

.在处理广播时,会为应用程序指定固定的时间(目前为10秒),以便在该时间内完成其工作.如果未在该时间内完成,则认为该应用程序行为不正常,并且如果需要,其进程会立即进入后台状态以杀死它以进行存储.

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.

在这里有一个使用示例服务的更好方法是使用意图服务.

its a better practice to use also intent service here you have a example how to do it.

这是广播接收器类.

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);
    }
}

并将其注册在清单中.

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

这是意图服务类.

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);
    }
}

并将其注册在清单中.

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

然后在您的活动中将警报管理器设置为在特定时间启动广播接收器,并使用AlarmManager setRepeating方法重复此示例,此示例将每天重复一次.

and then in your activity set the alarm manger to start the broadcast receiver at a specific time and use AlarmManager setRepeating method to repeat it this example bellow will repeat it every day.

 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);

我希望这会对您有所帮助.

i hope this will help you.

这篇关于如何在特定时间在android上发出通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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