2016-10-10 63 views
0

我期待为Android应用程序创建一个函数,在该应用程序中,每月的第25天我会收到通知,指示我必须执行某个任务。Android:在特定日期安排通知消息

我已经能够使用下面的代码显示通知:

public class NotificationPublisher extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    long[] pattern = {0, 300, 0}; 
    PendingIntent pi = PendingIntent.getActivity(context,, intent, 0); 
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.small_logo_ico) 
      .setContentTitle(context.getResources().getString(R.string.notification_title)) 
      .setContentText(context.getResources().getString(R.string.notification_content)) 
      .setVibrate(pattern) 
      .setAutoCancel(true); 

    mBuilder.setContentIntent(pi); 
    mBuilder.setDefaults(Notification.DEFAULT_SOUND); 
    mBuilder.setAutoCancel(true); 
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(, mBuilder.build()); 
} 

}

现在,当我有我的应用程序打开并不允许我显示这个这个系统只适用当应用程序关闭时。我搜索了四处,并来到这个: Android notification at specific date 试了一下(时间表部分)后,我注意到,它关闭应用程序时不起作用,因为我得到一个关于注销Receiver的错误,这样做(取消注册)导致接收器被取消,并且不能显示通知。用于日程

代码:

NotificationPublisher receiver = new NotificationPublisher(); 
    this.receiver = receiver; 
    IntentFilter filter = new IntentFilter("ALARM_ACTION"); 
    registerReceiver(receiver, filter); 

    Intent intent = new Intent("ALARM_ACTION"); 
    intent.putExtra("param", "My scheduled action"); 
    PendingIntent operation = PendingIntent.getBroadcast(this, 0, intent, 0); 
    // I choose 15s after the launch of my application 
    alarms.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+15000, operation) ; 

有什么我失踪,或我使用了错误的方法来安排在特定日期的通知? (当前通知设定为将来预定15秒,这仅用于测试,我有一个功能准备在某个日期显示此功能)

+1

http://stackoverflow.com/questions/31086226/show-a-notification-on-a-particular-date-and-time – HAXM

回答

0

这是用来通知月中。也许你可以从下面的代码中获得。

Calendar calendar = Calendar.getInstance(); 
     calendar.set(Calendar.DAY_OF_MONTH, 15); 
     calendar.set(Calendar.HOUR_OF_DAY, 12); 
     calendar.set(Calendar.MINUTE, 0); 
     calendar.set(Calendar.SECOND, 0); 
     calendar.set(Calendar.MILLISECOND, 0); 
     if (calendar.getTimeInMillis() < System.currentTimeMillis()) { 
      calendar.add(Calendar.DAY_OF_YEAR, 30); 
     } 
     Intent myIntent = new Intent(getApplicationContext(), MyReceiver.class); 
     myIntent.putExtra("NOTI_MSG",getString(R.string.notification_sidas)); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), NOTI_REQ_CODE_SIDAS, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
       AlarmManager.INTERVAL_DAY * 30, pendingIntent); 
    } 
+0

我已经改变了最后一部分:“alarmManager.setRepeating( AlarmManager.RTC,System.currentTimeMillis()+ 3000, 2000,pendingIntent);“并保持其余的不变(当然,必要时改变引用),为什么这种方式无法正常工作? – Gerton