如何从 IntentService class 更新 edittext?

How to update edittext from IntentService class?

我正在制作一个聊天应用程序并使用意图服务向 firebase 数据库发送消息,即使在 activity 被销毁后也是如此。上传成功后,我想清除 IntentService 中 Edittext 中的文本 class 但不知道该怎么做。

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.

Intent 服务在 background 线程上运行,而不是在 main(UI) 线程上运行,但我们可以使用 Handler 机制来send/update 数据到 activity。 要将数据发送到 activity,您需要在 activity 中声明处理程序,例如:

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

在意图服务中class以这种方式传递数据:

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");
        }
    }

最重要的是从 activity 调用意图服务以将处理程序传递给它:

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

希望对您有所帮助

简单的解决方案是使用 Broadcast Reciever 在您的 activity class

中声明
BroadcastReceiver broadCastNewMessage = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // do your stuff here
            edittext.setText("");

        }
    };

现在 onCreate() 注册这个

registerReceiver(this.broadCastNewMessage, new IntentFilter("uploading_done"));

并且在onDestroy()

unregisterReceiver(broadCastNewMessage);

现在从您要更新 activity

服务 class 调用此方法
sendBroadcast(new Intent().setAction("uploading_done"));