2016-07-02 52 views
2

我見過一個簡單的應用程序,在按下按鈕5秒後安排通知。但我真正想要做的是將通知安排在特定的時間(例如,通知在00:00顯示),但我不知道如何設置那一刻(因爲現在我正在使用「延遲」)。 下面的代碼:日程安排通知android

public class MainActivity extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.action_5: 
      scheduleNotification(getNotification("5 second delay"), 5000); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

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

private Notification getNotification(String content) { 
    Notification.Builder builder = new Notification.Builder(this); 
    builder.setContentTitle("Scheduled Notification"); 
    builder.setContentText(content); 
    builder.setSmallIcon(R.mipmap.ic_launcher); 
    return builder.build(); 
} 

} 

而且類NotificationPublisher

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

} 
} 

這將是即使我知道如何安排,以便在通知的通知在00:00

回答

2

下面是相同的代碼:

public static void setNotificationScheduler(Context context) { 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new Intent(context, NotificationPublisher.class); 
    PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 999999, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    // Set the alarm to start at 00:00 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.set(Calendar.HOUR_OF_DAY, 0); 
    calendar.set(Calendar.MINUTE, 0); 

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent); 
} 
1
日常出現更好

我以前遇到過這個問題。所以我很樂意幫助你,所以你不會浪費太多時間,因爲我會盡可能簡單和簡潔。

Get the Alarm service from the system. 
Make a pending intent, passing in the broadcast receiver class's name. 
Make a calendar object and set the time of it too 8 am. 
Check if the current time is past 8. If yes then add another day to it. 
Call the set repeating method of AlarmManager class. 

對同一樣品代碼:

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmIntent = new Intent(context of current file, AlarmReceiver1.class); // AlarmReceiver1 = broadcast receiver 

      pendingIntent = PendingIntent.getBroadcast( Menu.this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
      alarmIntent.setData((Uri.parse("custom://"+System.currentTimeMillis()))); 
      alarmManager.cancel(pendingIntent); 

      Calendar alarmStartTime = Calendar.getInstance(); 
      Calendar now = Calendar.getInstance(); 
      alarmStartTime.set(Calendar.HOUR_OF_DAY, 8); 
      alarmStartTime.set(Calendar.MINUTE, 00); 
      alarmStartTime.set(Calendar.SECOND, 0); 
      if (now.after(alarmStartTime)) { 
       Log.d("Hey","Added a day"); 
       alarmStartTime.add(Calendar.DATE, 1); 
      } 

      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 
      Log.d("Alarm","Alarms set for everyday 8 am."); 


    Coming to the broadcast receiver class. You need to register your broadcast receiver in the manifest. This will cause you to receive clock events. 
    Override the onReceive method of this broadcast receiver and make a notification there itself or make a seperate notification building service and build and display your notification there. 


The manifest code snippet: 

    <receiver android:name="AlarmReceiver1" android:enabled="true"> 


The broadcast receiver code snippet: 

    public class AlarmReceiver1 extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      Intent service1 = new Intent(context, NotificationService1.class); 
     service1.setData((Uri.parse("custom://"+System.currentTimeMillis()))); 
        context.startService(service1); 
    } 


Notification building service code snippet: 

    public class NotificationService1 extends IntentService{ 

     private NotificationManager notificationManager; 
     private PendingIntent pendingIntent; 
     private static int NOTIFICATION_ID = 1; 
     Notification notification; 
    @Override 
     protected void onHandleIntent(Intent intent) { 
    ontext context = this.getApplicationContext(); 
       notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Intent mIntent = new Intent(this, Activity to be opened after clicking on the notif); 
       Bundle bundle = new Bundle(); 
       bundle.putString("test", "test"); 
       mIntent.putExtras(bundle); 
       pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);  

       Resources res = this.getResources(); 
       NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
       Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
       notification = new NotificationCompat.Builder(this) 
          .setContentIntent(pendingIntent) 
          .setSmallIcon(R.drawable.ic_launcher) 
          .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher)) 
          .setTicker("ticker value") 
          .setAutoCancel(true) 
          .setPriority(8) 
          .setSound(soundUri) 
          .setContentTitle("Notif title") 
          .setContentText("Text").build(); 
       notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS; 
       notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE; 
       notification.ledARGB = 0xFFFFA500; 
       notification.ledOnMS = 800; 
       notification.ledOffMS = 1000; 
       notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
       notificationManager.notify(NOTIFICATION_ID, notification); 
       Log.i("notif","Notifications sent."); 

     } 

    } 

爲了幫助有關代碼,然後它可以在這裏找到:-http://www.singhajit.com/schedule-local-notification-in-android/ 你去那裏。而已。 希望這有助於。

2

你在找什麼是RTC_WAKEUP

而不是使用一個延遲的,你可以做到以下幾點:

Calendar calendar = Calendar.getInstance(); 

calendar.setTimeInMillis(System.currentTimeMillis()); 
calendar.set(Calendar.HOUR_OF_DAY, 0); 
calendar.set(Calendar.MINUTE, 0); // For 00:00 

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

您可以在這裏找到更多的信息:https://developer.android.com/training/scheduling/alarms.html