2016-04-01 14 views
0

私は先週まで完全に動作していたアラームをアラームに設定しました。私が使用したコードはこれでしたアラームと時刻の変更を繰り返す

am.setRepeating(AlarmManager.RTC_WAKEUP、setalarmon.getTimeInMillis()、7000 * 3600 * 24、pi);

現地時間の11:00に毎週金曜日にアラームを設定します。問題は私の国がDSTの他のタイムゾーン原因に移動したことです。 UTC + 1になったので、UTC + 2になりました。アラームが現在12:00に発砲しています。 DSTの変更に関係なく、同時にアラーム発火を設定する方法はありますか?

答えて

0

私は正確に何を知りませんsetalarmonです。私はそのカレンダーオブジェクトを推測する。あなたはそれをどう定義したのですか?

Calendar setalarmon = Calendar.getInstance(Locale.getDefault());

あなたは右のタイムゾーンを与える必要があります。

0

はいこれはカレンダーオブジェクトで、正しい現地時間を使用します。私の問題はsetRepeatingはUTC時間を使用しています。ここに何が起こったのですか? 3月25日金曜日11:00に発覚した。それは4月1日金曜日11:00に再び発火するはずですので、7 * 24 * 60 * 60 * 1000ミリ秒後に繰り返すように正しく設定されています。問題はDSTのために変更された現地時間であり、これらのミリ秒後に現地時間は12時間ではなく11時間であるため、アラームは12時に発砲している。それは避けたいものだから助けを求めている。

0

試してみよう

MainActivity:

public static int NOW_NOTIFICATION = 101; 

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

       cancelNotification(NOW_NOTIFICATION); 
       AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
       alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() 
         + (2 * 1000), pendingIntent); 

キャンセル通知:

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

NotificationReceiver:

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


    @Override 
    public void onReceive(Context context, Intent intent) { 
     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 + ", Current Time : " + dateTime.getHourOfDay() + ":" + dateTime.getMinuteOfHour() + ":" + dateTime.getSecondOfMinute()); 
     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); 
    } 

} 

TimeZoneChangedReceiver:

public class TimeZoneChangedReceiver 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; 
     Intent intentNitification = new Intent(context, NotificationReceiver.class); 
     intentNitification.putExtra(NotificationReceiver.NOTIFICATION_MESSAGE, "Sample Alert"); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(
       context, NOW_NOTIFICATION, intentNitification, PendingIntent.FLAG_ONE_SHOT); 

     cancelNotification(NOW_NOTIFICATION); 
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() 
       + (2 * 1000), pendingIntent); 
    } 

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

AndroidManifiest:

<receiver android:name=".NotificationReceiver" /> 

     <receiver android:name=".TimeZoneChangedReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.TIMEZONE_CHANGED" /> 
      </intent-filter> 
     </receiver> 
関連する問題