1

В моем проекте нужно показать уведомление каждый день в 2 часаМестного уведомлению каждый день в определенное время в андроиде

Я понятие не имею, о настройке временной интервал для повторного уведомления каждого день

Моего главного кодовой активности интервал времени

Intent myIntent = new Intent(Splash.this ,JobsNotification.class);  
     AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
     PendingIntent pendingIntent = PendingIntent.getService(Splash.this, 0, myIntent, 0); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.set(Calendar.HOUR_OF_DAY,12); 
     calendar.set(Calendar.MINUTE,26); 
     calendar.set(Calendar.SECOND,15); 
     alarmManager.setRepeating(AlarmManager.RTC, 
       calendar.getTimeInMillis() , AlarmManager.INTERVAL_DAY, 
       pendingIntent); 

Сервис для уведомления

public class JobsNotification extends Service 
    { 
    @Override 
    public IBinder onBind(Intent arg0) 
    { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onCreate() 
    { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
    } 

    @SuppressWarnings("deprecation") 
@Override 
    public void onStart(Intent intent, int startId) 
    { 
     super.onStart(intent, startId); 

     String strTitle="10 New Jobs Posted Today"; 


    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.dailyjob) 
      .setContentTitle(strTitle) 
      .setContentText(""); 
    Intent resultIntent = new Intent(this, Notify.class); 

    // The stack builder object will contain an artificial back stack for the 
    // started Activity. 
    // This ensures that navigating backward from the Activity leads out of 
    // your application to the Home screen. 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
    // Adds the back stack for the Intent (but not the Intent itself) 
    stackBuilder.addParentStack(Notify.class); 
    // Adds the Intent that starts the Activity to the top of the stack 
    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent resultPendingIntent = 
      stackBuilder.getPendingIntent(
       0, 
       PendingIntent.FLAG_UPDATE_CURRENT 
      ); 
    mBuilder.setContentIntent(resultPendingIntent); 
    mBuilder.setAutoCancel(true); 
    NotificationManager mNotificationManager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    // mId allows you to update the notification later on. 
     mNotificationManager.notify(1, mBuilder.build()); 
    // Uri notificationtune = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    // Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notificationtune); 
    // r.play(); 
    } 

    @Override 
    public void onDestroy() 
    { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
    } 

} 

Пожалуйста, помогите мне о настройке уведомлений каждый день только определенное время

Заранее спасибо

+0

это настройка уведомления за один раз? – Salmaan

ответ

-3

Каждый день в 2, когда приходит уведомление, установить уведомление на следующий день ...
Это единственный способ вы можете получить вечные уведомления :)

+0

Благодарим за повтор; я не понимаю, как настроить повторную тревогу, и не на мое уведомление приходит каждый запуск приложения –

0

Попробуйте этот код

// get a Calendar object with current time 
     Calendar cal = Calendar.getInstance(); 
     // add 5 minutes to the calendar object 
     cal.add(Calendar.SECOND, 10); 

     Intent intent = new Intent(this, AlarmReceiver.class); 
     intent.putExtra("alarm_message", "O'Doyle Rules!"); 

     // In reality, you would want to have a static variable for the request code instead of 192837 
     PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    // Get the AlarmManager service 
     AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
     // am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5, sender); 

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver 
    { 

     @Override 
     public void onReceive(Context context, Intent intent) 
     { 
      try 
      { 
       Bundle bundle = intent.getExtras(); 
       String message = bundle.getString("alarm_message"); 
       Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
      } 
      catch (Exception e) 
      { 
       Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show(); 
       e.printStackTrace(); 

      } 
     } 

} 

AndroidManifest.xml

добавить разрешение на

<receiver android:name=".view.AlarmReceiver" > 
     </receiver> 

Установите определенное время в календаре.

+0

http://justcallmebrian.com/2010/04/27/using-alarmmanager-to-schedule-activities-on-android/ –

+0

Заменить тост уведомлением. – Hemant

+0

Спасибо за повтор, мы изменили время 'am.setRepeating (AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5, отправитель);' но Toast не наступает –

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