2016-04-27 1 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

私はこれを使用している24時間 – Pooya

+0

http://paste.ofcode.org/iDRAvSGaGBCBYZMv6aT9JQのために3つの別々の繰り返しのアラームをそれぞれ設定するが、私はにに問題に直面しています:私は、これはあなたが

NotificationReceiverが助けになると思います私のローカルデータベースから来ている時間を設定してください[ –

+0

あなたの時間はどのパターンにも従っていません。 forループではなく別のアラームを使用する必要があります – Pooya

答えて

0

ここは私のサンプルコードです。

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