Search code examples
androidandroid-intentandroid-intentservice

How to update edittext from IntentService class?


I'm making a chat app and using intent service for sending messages to firebase database even after activity is destroyed. When the uploading is successful i want to clear text in Edittext in the IntentService class but don't know how to do it.

Acivity class code

Intent sendmsgService = new Intent(getApplicationContext(),SendMessageService.class);
        sendmsgService.putExtra("msg",message);
        sendmsgService.putExtra("time",time);
        sendmsgService.putExtra("cuser",current_user);
        sendmsgService.putExtra("otheruser",otherusername);
        startService(sendmsgService);

IntentService class code

    public class SendMessageService extends IntentService {
DatabaseReference msgDatabase , mDatabase;
String current_user,otherusername,message,time;

public SendMessageService() {
    super("SendMessageService");
    mDatabase = FirebaseDatabase.getInstance().getReference();
    msgDatabase = mDatabase.child("messages").child(current_user).child(otherusername);
}

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    assert intent!=null;
    message = intent.getStringExtra("msg");
    time = intent.getStringExtra("time");
    current_user = intent.getStringExtra("cuser");
    otherusername = intent.getStringExtra("otheruser");
    DatabaseReference push_database =  msgDatabase.push();
    String msg_push = push_database.getKey();
    DatabaseReference d = FirebaseDatabase.getInstance().getReference();
    String cuser = current_user+"/"+otherusername+"/";
    String ouser = otherusername+"/"+current_user+"/";
    Map<String,Object> chatitems = new HashMap<>();
    chatitems.put("seen",false);
    chatitems.put("msg",message);
    chatitems.put("from",current_user);
    Map<String,Object> msgitemmap = new HashMap<>();
    chatitems.put("time", ServerValue.TIMESTAMP);
    msgitemmap.put("servertime",ServerValue.TIMESTAMP);
    msgitemmap.put("time",time);
    msgitemmap.put("msg",message);
    msgitemmap.put("from",current_user);
    msgitemmap.put("to",otherusername);
    msgitemmap.put("seen",false);
    msgitemmap.put("key",msg_push);
    Map<String,Object> chatmap = new HashMap<>();
    chatmap.put("chatlist/"+cuser,chatitems);
    chatmap.put("chatlist/"+ouser,chatitems);
    chatmap.put("messages/"+cuser+msg_push,msgitemmap);
    chatmap.put("messages/"+ouser+msg_push,msgitemmap);
    d.updateChildren(chatmap).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {

               //Here i want to clear the edittext 

        }
    });
}}

If any other solution please suggest.


Solution

  • Intent service runs on background thread not on main(UI) thread but we can use the Handler mechanism to send/update data to activity. To send data to activity you need to declare handler in your activity like:

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
                Bundle reply = msg.getData();
                                // do whatever with the bundle here
                }
    };
    

    in the intent service class pass data in this way:

    Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Messenger messenger = (Messenger) bundle.get("messenger");
            Message msg = Message.obtain();
            msg.setData(data); //put the data here
            try {
                messenger.send(msg);
            } catch (RemoteException e) {
                Log.i("error", "error");
            }
        }
    

    Most Important invoke the intent service from activity to pass handler to it:

    Intent intent = new Intent(this, IntentService1.class);
            intent.putExtra("messenger", new Messenger(handler));
            startService(intent);
    

    Hope this will help you