2017-10-11 94 views
0

我想做一个简单的应用程序,每天在不同的时间发送通知(在后台)。我已经搜索了如何在互联网上做到这一点,但我是新来的android编程,我不明白很多。所以有人可以帮助我,我不想要所有的代码,但只需要一个关于如何去做的基本想法。 感谢您的帮助。应用程序在给定时间在后台发送通知

回答

0

查找NotificationManager和AlarmManager。

实施例:https://gist.github.com/BrandonSmith/6679223

:取自

public class NotificationPublisher extends BroadcastReceiver { 

    public static String NOTIFICATION_ID = "notification-id"; 
    public static String NOTIFICATION = "notification"; 

    public void onReceive(Context context, Intent intent) { 

    NotificationManager notificationManager = (NotificationManager) 
     context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = intent.getParcelableExtra(NOTIFICATION); 
    int id = intent.getIntExtra(NOTIFICATION_ID, 0); 
    notificationManager.notify(id, notification); 

    } 
} 

private void scheduleNotification(Notification notification, int delay) { 

    Intent notificationIntent = new Intent(this, NotificationPublisher.class); 
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1); 
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    long futureInMillis = SystemClock.elapsedRealtime() + delay; 
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); 
} 

代码

相关问题