我想使用廣播接收器每24小時更新一次變量的值,即使經過大量研究,也無法知道如何更新變量數據。從廣播接收器發送數據到片段android
這是我用來每24小時調用一次鬧鐘的功能,並將需要增加的變量發送到鬧鐘接收器,這兩個鬧鐘都工作正常。
public void scheduleAlarm() {
Intent intentAlarm= new Intent(getActivity(), AlarmReciever.class);
intentAlarm.putExtra("imageName",""+imagename); // variable to be updated
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, 4);
calSet.set(Calendar.MINUTE, 18);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
if(calSet.compareTo(calNow) <= 0){
calSet.add(Calendar.DATE, 1);
}
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),AlarmManager.INTERVAL_DAY, PendingIntent.getBroadcast(getActivity(), 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
}
做了一些研究之後,我一直無法理解如何轉移到檢索到的值並遞增以返回到片段。這是我的接收者班。
public class AlarmReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("servicerun","true");
String intentImageName = intent.getStringExtra("imageName");
int numberImageName = Integer.parseInt(intentImageName) +1; // How to send this value back to the fragment?
}
}
任何幫助,將不勝感激。
檢查此鏈接http://android-er.blogspot.in/2015/04/example-of-using-alarmmanager-to.html – darwin