我试图使用广播接收器每24小时更新片段中的变量值,即使经过大量研究,也无法理解如何更新变量数据。
这是我用来每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?
}
}
任何帮助都将不胜感激。