2015-10-10 2 views
0

Я хочу установить уведомление, которое будет отображаться в определенное время, например, 10 утра. Я уже искал это в Интернете и делал это, но я не знаю, что не так, потому что ничего не уведомляет! Вот мой класс уведомления:Установить уведомление на определенное время

public class notification extends AppCompatActivity { 
    NotificationManager manager; 
    Notification myNotication; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_notification); 
     manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     Intent intent = new Intent(notification.this,about.class); 

     PendingIntent pendingIntent = PendingIntent.getActivity(notification.this, 1, intent, 0); 

     Notification.Builder builder = new Notification.Builder(notification.this); 

     builder.setAutoCancel(false); 
     builder.setTicker("this is ticker text"); 
     builder.setContentTitle("WhatsApp Notification"); 
     builder.setContentText("You have a new message"); 
     builder.setSmallIcon(R.drawable.up); 
     builder.setContentIntent(pendingIntent); 
     builder.setOngoing(true); 
     builder.setNumber(100); 
     myNotication = builder.getNotification(); 
     manager.notify(0, myNotication); 
    } 

И мой основной класс (активность) (OnCreate метод):

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     alarmMgr0 = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
     Intent intent = new Intent(main.this,notification.class); 
     intent.putExtra("uur", "1e"); 
     pendingIntent = PendingIntent.getBroadcast(main.this, 0, intent, 0); 
     timeOff9 = Calendar.getInstance(); 
     timeOff9.set(Calendar.HOUR_OF_DAY, 01); 
     timeOff9.set(Calendar.MINUTE, 46); 
     timeOff9.set(Calendar.SECOND, 0); 
     alarmMgr0.set(AlarmManager.RTC_WAKEUP, timeOff9.getTimeInMillis(), pendingIntent); 

ответ

0

я думаю, вы должны изменить этот код, приведенный ниже, как ваше требование

private void handleNotification() { 
    Intent alarmIntent = new Intent(this, AlarmReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5000, pendingIntent); 
} 

и это пользовательское значение BroadcastReceiver

public class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Calendar now = GregorianCalendar.getInstance(); 
     int dayOfWeek = now.get(Calendar.DATE); 
     if(dayOfWeek != 1 && dayOfWeek != 7) { 
      NotificationCompat.Builder mBuilder = 
        new NotificationCompat.Builder(context) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setContentTitle(context.getResources().getString(R.string.message_box_title)) 
        .setContentText(context.getResources().getString(R.string.message_timesheet_not_up_to_date)); 
      Intent resultIntent = new Intent(context, MainActivity.class); 
      TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
      stackBuilder.addParentStack(MainActivity.class); 
      stackBuilder.addNextIntent(resultIntent); 
      PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
      mBuilder.setContentIntent(resultPendingIntent); 
      NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
      mNotificationManager.notify(1, mBuilder.build()); 
     } 

    } 
} 

И добавить манифест приложения тег:

<receiver 
     android:name="be.menis.timesheet.service.AlarmReceiver" 
     android:process=":remote" /> 

изменить этот код, как ваши потребности я думаю, что это полезно

+0

Спасибо за ответ на мой API лвл 14, и как я могу установить его на определенное время? –

Смежные вопросы