2016-04-27 3 views
0
public class SchedulerSetupReceiver extends WakefulBroadcastReceiver { 
    private static final String APP_TAG = "com.hascode.android"; 

    private static final int EXEC_INTERVAL = 10 * 1000; 
    DBhelper dbhelper; 

    @Override 
    public void onReceive(final Context ctx, final Intent intent) { 
     Log.d(APP_TAG, "SchedulerSetupReceiver.onReceive() called"); 
     dbhelper = new DBhelper(ctx); 


     String [] ID=dbhelper.FetchAllID(); 
     String[] time=dbhelper.FetchAlltime(); 

     for(int k=0;k<ID.length;k++) 
     { 

     AlarmManager alarmManager = (AlarmManager) ctx 
       .getSystemService(Context.ALARM_SERVICE); 
     Intent i = new Intent(ctx, SchedulerEventReceiver.class); // explicit 
                    // intent 
     PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx, 0, i, 
       PendingIntent.FLAG_CANCEL_CURRENT); 
     Calendar now = Calendar.getInstance(); 
     now.add(Calendar.SECOND, 20); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
       now.getTimeInMillis(), EXEC_INTERVAL, intentExecuted); 


     } 

    } 

} 

Это мой код для повторения сигнала тревоги в каждом 20 второй, но я хочу сигнализации следует установить в 3.30, 4,50, 8,30 может предложить любой из меня, что я изменю в наборе повторяющихся метод, так что Alarm будет широко распространен. Каждый 3.30, 4.50.8.30, чтобы я мог проверить некоторое состояние в широковещательном режиме.Как установить сигнализацию на данный момент времени в андроида

+0

набор 3 отдельные повторяющиеся сигналы каждые 24 часа – Pooya

+0

http://paste.ofcode.org/iDRAvSGaGBCBYZMv6aT9JQ я есть использовать это Но я столкнулся проблемы с в установите мое время, которое исходит из моего локального времени базы данных [] –

+0

ваше время не соответствует ни одному шаблону. вы должны использовать отдельный аварийный сигнал для каждого из них, а не в цикле for – Pooya

ответ

0

Вот мой пример кода. Я думаю, что это поможет вам

NotificationReceiver:

public class NotificationReceiver extends BroadcastReceiver { 
    public static String NOTIFICATION_MESSAGE = "notificationMessage"; 
    public static int NOW_NOTIFICATION = 101; 

    Context context; 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     this.context = context; 

     String message = intent.getStringExtra(NOTIFICATION_MESSAGE); 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     //Define sound URI 
     Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     Intent notificationIntent = new Intent(context, EmptyActivity.class); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     final DateTime dateTime = DateTime.now(); 
     int color = 0xffffaa00; 
//  int color1 = context.getColor(R.color.notificatinBackgroundColor); 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
     builder.setSmallIcon(R.drawable.festi_push_message_small); 
     builder.setContentIntent(pendingIntent); 
     builder.setContentTitle("Notification Sample"); 
     builder.setAutoCancel(true); 
     builder.setContentText(message); 
     builder.setSound(soundUri); 
     builder.setLights(Color.RED, 1000, 1000); 
     builder.setColor(color); 

     Notification notification = builder.build(); 

     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     notificationManager.notify(102938, notification); 

     cancelNotification(NOW_NOTIFICATION); 
     createNewNotification(); 
    } 

    public void cancelNotification(int requestCode) { 
     try { 
      Intent notificationIntent = new Intent(context, NotificationReceiver.class); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
      AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
      alarmManager.cancel(pendingIntent); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private void createNewNotification() { 

     //Set next alarm date time (dynamically coming date and time) here 
     final DateTime dateTime1 = DateTime.now(); 
     dateTime1.hourOfDay().setCopy(7); 
     dateTime1.minuteOfHour().setCopy(30); 
     dateTime1.secondOfMinute().setCopy(00); 
     final Calendar calendarNotifiedTime1 = Calendar.getInstance(); 
     calendarNotifiedTime1.set(Calendar.HOUR, dateTime1.getHourOfDay()); 
     calendarNotifiedTime1.set(Calendar.MINUTE, dateTime1.getMinuteOfHour()); 
     calendarNotifiedTime1.set(Calendar.SECOND, dateTime1.getSecondOfMinute()); 
     calendarNotifiedTime1.set(Calendar.AM_PM, Calendar.AM); 

     Intent intent = new Intent(context, NotificationReceiver.class); 
     intent.putExtra(NotificationReceiver.NOTIFICATION_MESSAGE, "Sample Alert"); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(
       context, NOW_NOTIFICATION, intent, PendingIntent.FLAG_ONE_SHOT); 

     cancelNotification(NOW_NOTIFICATION); 
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendarNotifiedTime1.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 

    } 

} 
+0

. Класс DateTIme() не найден. –

+0

Я использую joda datetime. – Sabari

+0

@ResearchDevelopment Работает? – Sabari

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