与BufferedReader一起使用Toast

时间:2012-12-11 15:01:53

标签: java android tcp alert toast

我希望你能提供帮助。

基本上我想发送“BufferedReader”读取Toast的内容,但我之前从未使用过Toast,似乎无法理解它。

如下所示,BufferedReader为“In”。

提前致谢。

代码:

package com.smarte.smartipcontrol;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;

public class IPControl extends Activity {

private Socket socket;
private static final int REDIRECTED_SERVERPORT = 32;
public PrintWriter out;
public BufferedReader in;
public String data;
public Object pd;
Intent intent;
String actu_ip; 
String textStatus;

public void getModel(View view) {
    try {
        out.println("[m\r\n");
        //System.out.print("root\r\n");
        while(!in.ready());
        textStatus = readBuffer();

    } catch(IOException e) {
        e.printStackTrace();
    }
}


//get the message from intent   
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    actu_ip = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
    setContentView(R.layout.act_ipcontrol);

    try {
        new AsyncAction().execute();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

private class AsyncAction extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... args) { 
        try {
            InetAddress serverAddr = InetAddress.getByName(actu_ip);
            socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
            OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
            BufferedWriter bw = new BufferedWriter(osw);
            out = new PrintWriter(bw, true); 
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while (! in .ready());
            readBuffer();
            out.println("root\r\n");
            while (! in .ready());
            readBuffer();
            out.println("root\r\n");
            while (! in .ready());
            String msg = "";
            while ( in .ready()) {
                msg = msg + (char) in .read();
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;//returns what you want to pass to the onPostExecute()
    }

    protected void onPostExecute(String result) {

        //results the data returned from doInbackground

        IPControl.this.data = result;

    }
}

private String readBuffer() throws IOException {
    String msg = "";

    while(in.ready()) {
        msg = msg + (char)in.read();
    }
    //System.out.print(msg);
    if(msg.indexOf("SNX_COM> ") != -1) return msg.substring(0, msg.indexOf("SNX_COM> "));
    else if(msg.indexOf("SCX_COM> ") != -1) return msg.substring(0, msg.indexOf("SCX_COM> "));
    else return msg;
}
}

1 个答案:

答案 0 :(得分:2)

Toast只会在屏幕上显示一条消息

Toast.makeText(context, message to display, Toast.LENGTH_SHORT).show();

在您的IPControl活动中,将其放在onCreate中的某个位置,您将看到Toast的功能

Toast.makeText(IPControl.this, "Thanks for stopping by!", Toast.LENGTH_LONG).show();

onPostExecute()尝试

Toast.makeText(IPControl.this, result, Toast.LENGTH_LONG).show();