2016-04-08 9 views
0

私は自分のアプリに問題があります。最初の起動時には、alarmintentを設定したいと思います。アラームマネージャーは、特定の時刻に24時間ごとに通知を送信します。 初めてアプリを起動したとき(データがアプリからクリアされているとき)以外は、すぐに通知が送信されます。次回の時計が09: 00。ここで AlarmManagerは初期設定時に送信されます

アラームが

public void setAlarm(){ 
    Toast.makeText(this, "setAlarm()", Toast.LENGTH_LONG).show(); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.set(Calendar.HOUR_OF_DAY, 9); 
    calendar.set(Calendar.MINUTE, 0); 
    calendar.set(Calendar.SECOND, 0); 

    Intent alarmIntent = new Intent(this, AlertReceiver.class); 

    if(PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_ONE_SHOT) != null){ 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 
    } 
} 

(のみ初めてアプリが実行されていると呼ばれる)セットアップ機能であり、ここで

public void createNotification(Context context, String msg, String msgText, String msgAlert){ 
    PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); 
    mBuilder.setSmallIcon(R.drawable.ic_stat_dmq_notification_icon); 
    mBuilder.setTicker(msgAlert); //Ticker! 
    mBuilder.setWhen(System.currentTimeMillis()); 
    mBuilder.setContentTitle(msg); //Title: 
    mBuilder.setContentText(msgText); //Text 
    mBuilder.setContentIntent(notificIntent); 
    mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND); 
    mBuilder.setAutoCancel(true); 

    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(uniqueId, mBuilder.build()); 

は私が持っている放送受信機の通知機能でありますこれを数日間見ていると、私はそれを推測することができず、解決策を見つけることもできません。誰かが私を幸せにするのに役立つことができたら!

+1

calendar.setTimeInMillis (のSystem.currentTimeMillis()をコールする必要はありません;。 – peter

+0

うん、あなたは正しいですが、それは私のエラーを変更しなかった期待通り –

答えて

0

オーケーをテストするために私は結局それを把握するために管理します。追加する必要 だけの事はそう

if(calendar.getTimeInMillis() < System.currentTimeMillis()){ 
      Toast.makeText(this,"1 day have been added", Toast.LENGTH_SHORT).show(); 
      calendar.add(Calendar.DATE, 1); 
} 

それは基本的には時間が過去にあったかどうかを確認し、それがあった場合、それは別の日を追加し、ALLEたcalendar.set後のことでした次の日にアラームが消えます。

1

あなたの地域で午前9時過ぎですか?それがあなたのエラーの原因かもしれません。過去のアンドロイドで設定されたアラームはすぐに消えます。試してみてください:

calendar.set(Calendar.YEAR, 2017); 

非常識だけ

+0

あなたが正しいと、アラームは、オフに行くことはありません。 –

関連する問題