Vikas Soni Vikas Soni - 13 days ago 9
Android Question

How Update Fragment ListView From BroadcastReceiver Class

I am working a calling application , in my application i set a alarm task and in broadcastreceiver class after task completed i need to update fragment list view.

my fragment class..

public class CallScheduling extends Fragment implements View.OnClickListener {

TextView messageText;
FloatingActionButton AddScheduleCall;
RecyclerView SheduleCalled;
View view;

DataBase dataBase;
List<SchedueModel> schedueModels;

public CallScheduling() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_call_scheduling, container, false);
setHasOptionsMenu(true);
init();
return view;
}

private void init() {

messageText = (TextView) view.findViewById(R.id.messageText);
AddScheduleCall = (FloatingActionButton) view.findViewById(R.id.AddScheduleCall);
SheduleCalled = (RecyclerView) view.findViewById(R.id.SheduleCalled);
AddScheduleCall.setOnClickListener(this);

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
SheduleCalled.setLayoutManager(mLayoutManager);
SheduleCalled.setItemAnimator(new DefaultItemAnimator());


}

@Override
public void onResume() {
super.onResume();
dataBase = App.getInstance().getDatBase();
if (dataBase.checkForTables(Constant.TABLE_SCHEDULING)) {
messageText.setVisibility(View.GONE);
SheduleCalled.setAdapter(new ScheduleAdapter(getActivity()));
schedueModels = dataBase.GetAllScheduledCall();

Collections.sort(schedueModels, new Comparator<SchedueModel>() {
@Override
public int compare(SchedueModel schedueModel, SchedueModel t1) {
return schedueModel.getDate().compareTo(t1.getDate());
}
});


ActiveSchedul();

} else {

messageText.setVisibility(View.VISIBLE);
}

}

private void ActiveSchedul() {


for (int i = 0; i < schedueModels.size(); i++) {

if (schedueModels.get(i).getCompleted().equalsIgnoreCase("0")) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = sdf.parse(schedueModels.get(0).getDate());
} catch (ParseException e) {
e.printStackTrace();
}

long startDate = date.getTime();

Intent intentAlarm = new Intent(getActivity(), SchecduleBroadcast.class);
intentAlarm.putExtra("data", schedueModels.get(i));
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, startDate, PendingIntent.getBroadcast(getActivity(), 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
break;
} else {
boolean deleted = dataBase.deleteUser(schedueModels.get(i).getNumber(), Constant.TABLE_SCHEDULING, Constant.NUMBER);
if (deleted) {
schedueModels.remove(i);
SheduleCalled.getAdapter().notifyDataSetChanged();
}
}

}


}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.AddScheduleCall:

startActivity(new Intent(getActivity(), ScheduleForm.class));

break;

default:

break;
}
}


and this is my broadcastreceiver...

public class SchecduleBroadcast extends BroadcastReceiver {

SchedueModel schedueModel;

@Override
public void onReceive(Context context, Intent intent) {


if (intent.getSerializableExtra("data") != null) {
schedueModel = (SchedueModel) intent.getSerializableExtra("data");
Toast.makeText(context, schedueModel.getNumber() + " Is Completed ! Thanx", Toast.LENGTH_SHORT).show();
schedueModel.setCompleted("1");
App.getInstance().getDatBase().updateScheduledCall(schedueModel);
// new Home().updateFragment(CallScheduling.class, null);


}

}
}

Answer

Step 1: An inner BroadcastReceiver class

class MyBroadCastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    }
}

Step 2: Register the receiver

@Override
    protected void onResume() {
        super.onResume();
        if (!receiverResgistered) {
            registerReceiver(myBroadCastReceiver, new IntentFilter("intent.filter.data"));
            receiverResgistered = true;
        }
    }

Step 3: Unregister Receiver

@Override
    protected void onPause() {
        super.onPause();
        if (receiverResgistered) {
            unregisterReceiver(myBroadCastReceiver);
            receiverResgistered = false;
        }
    }

Step 4: Broadcast the data from SchecduleBroadcast

Bundle bundle = new Bundle();
Intent i = new Intent("intent.filter.data");
i.putExtras(bundle);
sendBroadcast(i);

OR

You can make SchecduleBroadcast inner class of the fragment