2017-02-04 74 views
0

我想使用广播接收器每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? 
} 

}

任何帮助,将不胜感激。

+0

检查此链接http://android-er.blogspot.in/2015/04/example-of-using-alarmmanager-to.html – darwin

回答

0

更结构化的方法是让这些类松散耦合,并使用EventBus将它们链接在一起。它可以无缝地将数据作为事件传递给多个类。要查看使用情况,请查看此Answer

+0

数据将如何发回当数据已被接收并且现在需要被发回时,onEvent函数@OBX –

+0

从何处接收数据并从哪里接收数据? – OBX

+0

当使用事件总线的post方法从片段发送数据,然后使用'event.getmessage()'从Reciever类的onEvent方法中检索数据时。我需要更新该消息并将其发送回片段。 @OBX –