I used the broadcast that checks items from the server.And the broadcast is depending AlarmManager that every 2 seconds updated.
this my class for the interface:
public class PollServiceUtils {
private static RefreshLayout mRefreshLayout;
public interface RefreshLayout{
void updateUI(boolean isCatch);
}
public static void getRefreshAlarm(Context context) {
if (Connectivity.getNetworkInfo(context) != null) {
String changeRate = SharedPreferenceValues.getChangeRate(context);
List<Gold> listGold;
if (changeRate != null && !changeRate.isEmpty()) {
listGold = new ConnectionDovizCom().fetchGoldValues(context);
if (listGold.size() == 0) {
return;
}
SharedPreferenceValues.setValuesGold(context,listGold);
Intent i = MainPageActivity.newIntent(context, true);
PendingIntent pi = PendingIntent.getService(context, 1, i, PendingIntent.FLAG_UPDATE_CURRENT);
//get son change rate
String resultChangeRate = listGold.get(0).getmChangeRate();
Log.d(TAG, "RESULT CHANGE RESULT: " + resultChangeRate);
if (changeRate.equals(resultChangeRate)) {
Log.d(TAG, "Old Change Rate ");
} else {
Log.d(TAG, "New Change Rate ");
SharedPreferenceValues.setChangeRate(context, resultChangeRate);
mRefreshLayout.updateUI(true);
}
}
}
}
Then, I called the interface for activity (or fragment) for updating:
public class MainPageActivity extends AppCompatActivity implements View.OnClickListener, AllAsycnTask.AltınAsyncRefreshResponse,PollServiceUtils.RefreshLayout {
/*... a lot of code*/
@Override
public void updateUI(boolean isCatch) {
fm = getSupportFragmentManager();
fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.gold_layout, new GoldFragment());
fragmentTransaction.commit();
}
}
OR
for the fragment :
public class GoldFragment extends Fragment implements View.OnClickListener,PollServiceUtils.RefreshLayout {
/*... a lot of code*/
public void updateUIforRefresh(Context c) {
mGoldsList.clear();
mGoldsList = SharedPreferenceValues.getValuesGold(c);
getAltınListForAdapter(c,mGoldsList); // set all view again
}
@Override
public void updateUI(boolean isCatch) {
updateUIforRefresh(getContext());
}
}
My problem is that when this connection is done, the interface returns true for updating the fragment. But the logcat shows " Attempt to invoke the interface method on a null object reference for PollServiceUtils"
Do you know which details I snatch?