How to populate updated data in recyclerview from alarm service class in android?

Stevan
Stevan
38 Points
19 Posts

I'm using Alarm Manager to update recycler view on specific interval (say in 10 seconds). I'm able to update data in recycler data adapter but recycler view not getting updated.

public class UpdateService extends IntentService {
    public UpdateService() {
        super("UpdateService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // Do the task here
        
            Call<List<DataMessage>> userCall = MainActivity.serviceApi.getDataList();
            userCall.enqueue(new Callback<List<DataMessage>>() {
                @Override
                public void onResponse(Call<List<DataMessage>> call, Response<List<DataMessage>> response) {
                    if (response.isSuccessful()) {
                        if (response.body() != null) {
                            for (DataMessage DataMessage : response.body()) {
                                if (DataMessage.getMessageId() > 0)
                                    DummyContent.DataMessages.add(DataMessage);
                            }
                            MyDataRecyclerViewAdapter myDataRecyclerViewAdapter = new MyDataRecyclerViewAdapter(DummyContent.DataMessages);
                            myDataRecyclerViewAdapter.notifyDataSetChanged();
                            
                            // Here I want something like:                            
                            if (myDataRecyclerViewAdapter.getItemCount() > 2)
                                recyclerView.smoothScrollToPosition(myDataRecyclerViewAdapter.getItemCount() - 1);
                            
                            MainActivity.appPreference.showToast("message fetched");
                        } else {
                            MainActivity.appPreference.showToast("Error. message fetch Failed");
                        }
                    } else {
                        MainActivity.appPreference.showToast("Error. message Failed");
                    }
                }

                @Override
                public void onFailure(Call<List<DataMessage>> call, Throwable t) {
                    MainActivity.appPreference.showToast("Error. message Failed");
                }
            });
        }
    }
}

 

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class DataAlarmReceiver extends BroadcastReceiver {

    // Triggered by the Alarm periodically (starts the service to run task)
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, UpdateService.class);
        //i.putExtra("foo", "bar");
        context.startService(i);
    }
}

When updating data adapter in recycler fragment activity then working fine with following:

myDataRecyclerViewAdapter.notifyDataSetChanged();
// Here I want something like:    
if (myDataRecyclerViewAdapter.getItemCount() > 2)                                
   recyclerView.smoothScrollToPosition(myDataRecyclerViewAdapter.getItemCount() - 1);
                            

I want to know if we can get recyclerview object in alarm service class?

Views: 247
Total Answered: 1
Total Marked As Answer: 0
Posted On: 27-Dec-2019 21:56

Share:   fb twitter linkedin
I think, you should use broadcast-receiver. Broadcast from background service and Receive from activity.
 - Priya  22-Feb-2020 04:57
Answers
andro
andro
18 Points
9 Posts
         

You can use sendBroadcast() to send intent from Update Service:

Intent intent = new Intent("UpdateMessageIntent");
sendBroadcast(intent);

As:

public class UpdateService extends IntentService {
    public UpdateService() {
        super("UpdateService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // Do the task here
        
            Call<List<DataMessage>> userCall = MainActivity.serviceApi.getDataList();
            userCall.enqueue(new Callback<List<DataMessage>>() {
                @Override
                public void onResponse(Call<List<DataMessage>> call, Response<List<DataMessage>> response) {
                    if (response.isSuccessful()) {
                        if (response.body() != null) {
                            for (DataMessage DataMessage : response.body()) {
                                if (DataMessage.getMessageId() > 0)
                                    DummyContent.DataMessages.add(DataMessage);
                            }
                          
                            Intent intent = new Intent("UpdateMessageIntent");
                            sendBroadcast(intent)
                            
                            MainActivity.appPreference.showToast("message fetched");
                        } else {
                            MainActivity.appPreference.showToast("Error. message fetch Failed");
                        }
                    } else {
                        MainActivity.appPreference.showToast("Error. message Failed");
                    }
                }

                @Override
                public void onFailure(Call<List<DataMessage>> call, Throwable t) {
                    MainActivity.appPreference.showToast("Error. message Failed");
                }
            });
        }
    }
}

And from your fragment you can add receiver to receive broadcasted intent:

public class MyFragment extends Fragment {

    // TODO: Customize parameter argument names
    private static final String ARG_COLUMN_COUNT = "column-count";
    // TODO: Customize parameters
    private int mColumnCount = 1;
    private MyInterface mListener;
     private View view1;
    RecyclerView recyclerView;


    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                String string = bundle.getString("");
                int resultCode = bundle.getInt("");

            }
            // here call update adaptor method like myRecyclerViewAdapter.updateList();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getActivity().registerReceiver(receiver, new IntentFilter("UpdateMessageIntent"));
    }
    @Override
    public void onPause() {
        super.onPause();
        getActivity().unregisterReceiver(receiver);
    }
Posted On: 22-Feb-2020 05:39
 Log In to Chat