簡體   English   中英

Android Service-Activity 2方式溝通

[英]Android Service-Activity 2 way communication

在我的團隊的Android應用程序中,我有一個從啟動運行的服務,它與服務器通信以執行諸如登錄,注冊,在電話之間聊天和更新電話數據庫等操作。

我需要讓我的服務與雙向活動進行通信:例如,我正在處理登錄活動,用戶名和密碼是從應用程序屏幕上的文本字段中獲取的字符串,我已經能夠通過它們向服務器發送授權命令到服務器。

public void loginPressed(View v){
    usernameStr = usernameField.getText().toString();
    passwordStr = passwordField.getText().toString();

    if (!bound) return;
    Bundle b = new Bundle();
    Message msg = Message.obtain(null, ChatService.LOGIN);
    try {
        b.putString("username", usernameStr);
        b.putString("password", passwordStr);
        msg.setData(b);
        messenger.send(msg);
    }
    catch (RemoteException e) {

    }

這可以像我預期的那樣工作。 當服務器響應一條消息,說明登錄是否成功時,我需要它將消息傳遞回活動,以便我可以啟動主要活動,如果成功或提示重新進入,如果沒有。

我嘗試使用msg.replyTo字段來獲取返回信使以發回信息,但是當我運行應用程序時,它會以空指針異常關閉,我不知道為什么會發生這種情況。 這是代碼似乎是罪魁禍首:

private class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what) {
        case LOGIN:

            Bundle b = msg.getData();
            String username = b.getString("username");
            String password = b.getString("password");

            String loginMessage = TCPCall.login(username, password);
            connection.sendMessage(loginMessage);

            String loginReturn = connection.retrieveMessage();
            Message m;

            Scanner s = new Scanner(loginReturn);
            s.useDelimiter(",");
            String c = s.next();
            String status = s.next();
            String message = s.next();

            if (status.equals("OK")) {
                m = Message.obtain(null, LoginActivity.OK);
                try {
                    msg.replyTo.send(m);
                } catch (RemoteException e) {}
            }
            else {
                m = Message.obtain(null, LoginActivity.ERR);
                try {
                    msg.replyTo.send(m);
                } catch (RemoteException e) {}
            }
            break;

空指針似乎來自於

msg.replyTo.send(m);

兩種情況下的代碼行(登錄成功和登錄失敗)

任何幫助解決這個問題將不勝感激:)

正如格雷格在評論中指出的那樣。 你需要設置msg.replyTo = messenger; int他發送原始郵件的地方。

可以在此處找到一個示例: http//www.survivingwithandroid.com/2014/01/android-bound-service-ipc-with-messenger.html

我想你忘了通過服務包發送對Login Activity的響應。 所以,我在Messenger Service中做了一些更改

定義一個全局變量並在Incoming Handler中進行一些更改

static final int LOGIN_STATUS = 1;

private class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
    switch(msg.what) {
    case LOGIN:

        Bundle b = msg.getData();
        String username = b.getString("username");
        String password = b.getString("password");

        String loginMessage = TCPCall.login(username, password);
        connection.sendMessage(loginMessage);

        String loginReturn = connection.retrieveMessage();
        Message m = Message.obtain(null, LOGIN_STATUS);

        Scanner s = new Scanner(loginReturn);
        s.useDelimiter(",");
        String c = s.next();
        String status = s.next();
        String message = s.next();

        if (status.equals("OK")) {
            b.putString("responseC",c);
            b.putString("responseStatus",status);
            b.putString("responseMessage",message)

            m.setData(b);
            try {
                msg.replyTo.send(m);
            } catch (RemoteException e) {}
        }
        else {
           /*if something is wrong with username and password you can put 
           a toast*/

            }
        break;

現在我們必須在LoginActivity中捕獲此響應,並在Login Activity中使用IncomingHandler

class IncomingHandler extends Handler{

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case ChatService.LOGIN_STATUS:
                    String C = msg.getData().getString("responseC");
                    String Status = msg.getData().getString("responseStatus");
                    String Message = msg.getData().getString("responseMessage");

                    //Here is your response in LoginActivity, enjoy!!! 

                    break;

                default:
                    super.handleMessage(msg);
            }
        }
    }

final Messenger mMessenger = new Messenger(new IncomingHandler());

public void loginPressed(View v){
usernameStr = usernameField.getText().toString();
passwordStr = passwordField.getText().toString();

if (!bound) return;
Bundle b = new Bundle();
Message msg = Message.obtain(null, ChatService.LOGIN_SATUS,0,0);
try {
    b.putString("username", usernameStr);
    b.putString("password", passwordStr);
    msg.setData(b);
    msg.replyTo = mMessenger;
    messenger.send(msg);
}
catch (RemoteException e) {
    // In this case the service has crashed before we could even
    // do anything with it; we can count on soon being
    // disconnected (and then reconnected if it can be restarted)
    // so there is no need to do anything here.

}

這段代碼工作正常,希望它會對你有所幫助,謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM