2016-10-09 2 views
0

정수를 "매일"지정된 시간에 재설정해야합니다. 필자의 활동에서 Shared 환경 설정에 매일 값을 기록한 다음 알람 관리자와 브로드 캐스트 리시버로 값을 0으로 설정하려고합니다. 공유 환경 설정 외에 모든 것이 작동하며 값은 여전히 ​​그대로 유지됩니다. 공유 된 환경 설정에 대해 다른 컨텍스트를 가져와야한다고 생각합니다. 어떤 도움을 환영합니다. 정수를 특정 시간에 매일 재설정하십시오.

수신기 :

public void onReceive(Context context, Intent intent) { 

    final SharedPreferences shared = context.getSharedPreferences("Mydata", MODE_PRIVATE); 
    final SharedPreferences.Editor editor = shared.edit(); 
    int sum = shared.getInt("data1", 0); 
    Calendar c = Calendar.getInstance(); 
    String date = String.valueOf(c.get(Calendar.DATE)); 

    mainData.insert_data(date, sum); 
    int zero = 0; 
    editor.putInt("data1", zero); 
    editor.apply(); 

} 

알람 매니저 : 메인 활동에

Calendar calendar = Calendar.getInstance(); 
     calendar.set(Calendar.HOUR_OF_DAY, 22); 
     calendar.set(Calendar.MINUTE, 45); 
     calendar.set(Calendar.SECOND, 0); 
     calendar.set(Calendar.MILLISECOND, 0); 
     final Context context = this.getContext(); 
     PendingIntent pi = PendingIntent.getService(context, 0, 
       new Intent(context, MyAppReciever.class),PendingIntent.FLAG_UPDATE_CURRENT); 
     AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
       AlarmManager.INTERVAL_DAY, pi); 

공유 환경 설정 :

final SharedPreferences shared = this.getContext().getSharedPreferences("Mydata", MODE_PRIVATE); 
     final SharedPreferences.Editor editor = shared.edit(); 
     editor.putInt("data1", 0); 
     editor.putInt("month", 0); 
     final int today212 = shared.getInt("data1", 0); 
     final int mesec212 = shared.getInt("month", 0); 
     final int limit212 = shared.getInt("budget", 0); 
+0

당신이 BroadcastReceiver''에서'SharedPreferences'를 업데이트하는 방법은 무엇입니까? – mklimek

+0

예, 그게 문제라고 생각합니다 –

+0

나는 그렇게 생각하지 않습니다. 'Integer'가 업데이트되지 않았 음을 어떻게 확인합니까? – mklimek

답변

0

그래서 내가 만든 코드에 몇 가지 변화 PendingIntent requestCode를 추가했습니다.

public void scheduleTestAlarmReceiver(Context context) { 
    Intent receiverIntent = new Intent(context, MyAppReciever.class); 
    PendingIntent sender = PendingIntent.getBroadcast(context, 123456789, receiverIntent, 0); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY, 23); 
    calendar.set(Calendar.MINUTE, 58); 

    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
      AlarmManager.INTERVAL_DAY, sender); 
} 

당신이 그것을 (말의 onclick)를 일정 장소 :

Context cn= this.getContext(); 
     scheduleTestAlarmReceiver(cn); 

방송 수신기가 있어야한다 코드는 당신이 당신의 알람 설정이

장소처럼 보인다 위와 동일합니다. 또한 매니페스트에 수신기를 추가하는 것을 잊지 마세요 :

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