从活动访问服务中的线程内的函数(套接字通信)

时间:2013-05-30 11:03:46

标签: android multithreading sockets service android-activity

我正在努力让活动和服务之间的沟通得以发挥作用。

我是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;
    }


}

0 个答案:

没有答案