2017-02-04 70 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 –