如何从IntentService类更新edittext?

时间:2018-06-01 07:41:28

标签: android android-intent android-intentservice

我正在创建一个聊天应用程序,并使用意向服务将消息发送到firebase数据库,即使在活动被销毁之后也是如此。上传成功后,我想在IntentService类的Edittext中清除文本,但不知道如何操作。

  

Acivity类代码

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类代码

    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 

        }
    });
}}
  

如果有任何其他解决方案,请建议。

2 个答案:

答案 0 :(得分:0)

Intent服务在后台线程上运行而不在主(UI)线程上运行,但我们可以使用Handler机制将数据发送/更新到活动。 要将数据发送到活动,您需要在活动中声明处理程序,如:

Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
            Bundle reply = msg.getData();
                            // do whatever with the bundle here
            }
};
意图服务类中的

以这种方式传递数据:

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 intent = new Intent(this, IntentService1.class);
        intent.putExtra("messenger", new Messenger(handler));
        startService(intent);

希望这会对你有所帮助

答案 1 :(得分:0)

简单的解决方案是使用Broadcast Reciever在您的活动类中声明这个

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

现在从服务类调用此方法,您想要更新活动

sendBroadcast(new Intent().setAction("uploading_done"));