1

アクティビティとサービスの間の通信を機能させようとしています。

私はAndroidとJavaにかなり慣れていないので、YouTube :Dで自分で学びましたが、これを見つけたり解決したりできません。

最初に、イーサネット ソケットを処理するクラス スレッドでアクティビティを作成しました。しかし、アクティビティを離れて別のスレッドに切り替えると、スレッドが破棄されることを発見しました。スレッドを保持するために、サービス内にスレッドを含めました。しかし、今ではスレッド内の機能を使用できません(ステータスの送信/取得など)。イーサネット クラスのインスタンスをすべてのアクティビティに戻すにはどうすればよいですか。以下に、コードをクリップしました。

コードが機能して実行されます。しかし、 send() 関数または getConStatus() を呼び出したいときにクラッシュしました。

ホームドモティックスのクライアントサーバー通信に取り組んでいます。

// 私の主な活動

import java.io.IOException;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends Activity {

    //UI declerations
    Button button1, button2;
    EditText text1,text2;
    ProgressBar waiter;
    Ethernets eth;
    house house;
    Toast message;
    ProgressTask Task;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);
        text1 = (EditText)findViewById(R.id.editText1);
        text2 = (EditText)findViewById(R.id.editText2);
        waiter = (ProgressBar)findViewById(R.id.Waiter);

    }

    protected void onStop()
    {
        Intent intent = new Intent(this, Ethernet_Service.class);
        stopService(intent);
    }

    public void start(View view)
    {   
        eth.send("Turn ON");    // i cant USE this !! 
    }

    public void connect(View view) throws UnknownHostException, IOException
    {   
        String ip = null;
        int port = 0;

        ip = text1.getText().toString();
        port = Integer.parseInt(text2.getText().toString());

        Intent intent = new Intent(this, Ethernet_Service.class);
        startService(intent);

    }

}

// 私のサービス

package com.example.homeapp;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class Ethernet_Service extends Service{

    public Ethernet_Service() {

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startid)
    {

        Ethernets eth = new Ethernets("I9001", "192.168.1.101", 4000);
        eth.start();
        return startid;
    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

// 私のイーサネット スレッド クラス

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

import android.util.Log;

public class Ethernets extends Thread {

    Thread ethernetThread, recieveThread;
    Socket ethernetSocket;
    PrintStream PW;
    String IP;
    int PORT;
    boolean connect_status = false;

    String Client_Name;

    public Ethernets(String name, String ip, int port)
    {
        Client_Name = name;
        PORT = port;
        IP = ip;
    }

    public void run()
    {
        try
        {
            ethernetSocket = new Socket(IP,PORT);
            if(ethernetSocket.isConnected() == true)
            {   
                connect_status = true;
            }
            else{connect_status = false;}

            PW = new PrintStream(ethernetSocket.getOutputStream(),true);

        }catch(Exception ex){}

        recieveThread = new Thread(new Runnable() {

            @Override
            public void run() {

                String message;
                try 
                {
                    BufferedReader BR = new  BufferedReader(new InputStreamReader(ethernetSocket.getInputStream()));
                    while((message = BR.readLine()) != null)
                    {
                        System.out.println("message: "+message);
                    }
                } catch (IOException e) {e.printStackTrace();}

            }
        });     
        recieveThread.start();
    }


    public void send(String msg)
    {
        //protocol description
        byte[] buffer = {(byte) 0x02, (byte) 0x04, (byte) 'B', (byte) 0x02, 0x00, 0x00, (byte) 0x46 };

        try
        {
            Log.i("hi","hi");
            PW.write(buffer);
            PW.flush();
        } catch (IOException e) {e.printStackTrace();}
    }

    public boolean getconStatus()
    {
        return connect_status;
    }


}
4

0 に答える 0