0

「保留中」の状態だったため、追加情報を使用して質問を再構成しています。別の質問を削除します。

注文 (コマンド) を送信し、サーバーからの応答を受信できるコンソール (cmd など) であるアプリを構築しようとしています。

画面に表示されるのは、注文とサーバーの応答です。

この目的のために何をすべきですか?

で試していServiceます。ただし、サービスはネットワークでは機能しません。

AsyncTask最初のコマンドを送信すると終了します。そのため、注文を送信するたびに再接続する必要があります (非効率的ですか?)

IntentServiceアクティビティの寿命にバインドされています。そして、それと同じように終了Asynctaskします。

初めてサーバーに接続するスレッドを実行するにはどうすればよいですか。次に、スレッドにメッセージを送信するたびに、サーバーに接続し、サーバーから TextEdit の情報を取得しますか?


[保留中]状態の編集。さらに情報を追加する


ここに私が持っているものがあります:

  • Activitya (TextView端末プロンプトに使用)、a EditText(ユーザーが送信したいコマンドを入力できるようにする)、および aButtonを使用して送信します。
  • これActivityは、次のようにバインドされてServiceいます。

    ServiceConnection mConnection = new ServiceConnection() {
    
        public void onServiceDisconnected(ComponentName name) {
            mBounded = false;
            mServer = null;
        }
    
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBounded = true;
            StatusService.LocalBinder mLocalBinder = (StatusService.LocalBinder)service;
            mServer = mLocalBinder.getServerInstance();
    
    
            /* once it is bounded, update data */
            updateSensorsData();
    
    
        }
    };
    
    /* ---------------- LIFE CYCLE METHODS -------------------------*/
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_console);
    
        Intent mIntent = new Intent(this, StatusService.class);
        bindService(mIntent, mConnection, BIND_AUTO_CREATE);
    
        /* Scrollable TextView for Console Prompt*/
        TextView tvConsole = (TextView) findViewById(R.id.tvConsole);
        tvConsole.setMovementMethod(new ScrollingMovementMethod());
    }
    
  • Serviceサーバーと対話し、情報を に取得する必要があるTextViewため、次のように実装しました。

    public String sendMessage(String message) {
    
        /* HERE I SHOULD CONNECT TO THE SERVER, BUT I DONT KNOW HOW */
        String response;
    
        connect(server, port);
        sendReceiveCmdControl(message);
    
    
        return response;
    }
    
    protected String sendReceiveCmdControl(String msg) throws InterruptedException {
        String res;
    
    
        send(controlOut, msg+"\n");
        // wait for response
        try {
            waitFor(controlIn, msg);
        } catch (EOFException e) {
            Log.d("ERROR", "Remote connection closed.");
            return "error";
    
        } catch (IOException e) {
            Log.d("ERROR", "IO problem.");
            return "error";
        } // end try
        // read the rest
        try {
            res = controlIn.readLine();
        } catch (IOException e) {
            Log.d("ERROR", "IO problem.");
            return "error";
        } // end try
    
    
        return(res);
    } // end sendReceiveCmdControl()
    

controlOut は a PrintWriter、 controlIn aでありDataInputStream、どちらも次のように method で初期化さconnectれます。

   /* Connection I/O */
   public static Socket controlSocket = null;
   public static PrintWriter controlOut = null;
   public static DataInputStream controlIn = null;
   public static Socket imageSocket = null;
   public static PrintWriter imageOut = null;
   public static DataInputStream imageIn = null;

   protected void connect(String server, int port, boolean controlEnabled, boolean imageEnabled) {

        if (imageEnabled) {
            try {
                imageSocket = new Socket(server, port);
                imageOut = new PrintWriter(imageSocket.getOutputStream(), true);
                imageIn = new DataInputStream(new BufferedInputStream(imageSocket.getInputStream()));
                Log.d("INFO", "Connected to image socket correctly");
            } catch (UnknownHostException e) {
                disconnect();
                Log.d("ERROR", "Exception: " + e.getMessage());
                return;
            } catch (IOException e) {
                disconnect();
                Log.d("ERROR", "Exception: " + e.getMessage());
                return;
            }

        }

        if (controlEnabled) {
            try {
                controlSocket = new Socket(server, port+1);
                controlOut = new PrintWriter(controlSocket.getOutputStream(), true);
                controlIn = new DataInputStream(new BufferedInputStream(controlSocket.getInputStream()));
                Log.d("INFO", "Connected to control socket correctly");
            } catch (UnknownHostException e) {
                disconnect();
                Log.d("ERROR", "Exception: " + e.getMessage());
                return;
            } catch (IOException e) {
                disconnect();
                Log.d("ERROR", "Exception: " + e.getMessage());
                return;
            } 

        }

    }

だから私がやりたいことは次のとおりです。

  1. connectサービスを初めて実行するときにメソッドを使用してサーバーに接続します。
  2. リモート サーバーに注文を送信するたびmServer.sendMessage(command);に、Activity で行う必要があります。
  3. したがって、は RemoteとService対話し、応答を取得すると、は UI 更新のために への応答を取得します。ServerServiceActivity

Activity-Service-Thread/Whatever...私に知らせるよりも別の適切な方法がある場合は、助けてくれてありがとう. そして、あなたの忍耐に感謝します。

4

0 に答える 0