2 u013270296 u013270296 于 2013.12.24 22:56 提问

小弟第一次编android程序,有关socket,但是就是看不出来哪有问题

package com.example.warningsystem;

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

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.media.MediaPlayer;

public class MainActivity extends Activity implements Runnable {

private static final String Host="host";
private static final int Port = 9999;
private String msg = null; 
private Thread thread = null;
private String br_in;
BufferedReader br;
MediaPlayer  audioPlayer;
ImageView    myView;
Button       stopBtn;
boolean      isPlaying = false;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    try
    {
        Socket socket = new Socket (Host,Port);

        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        msg = br.readLine();

        if(msg != null)
        {
            play();
        }
        else{
            msg = br_in;
            if(msg != null )
            {
                play();
            }
        }

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



    stopBtn= (Button)    findViewById(R.id.button2);
    stopBtn.setOnClickListener(new View.OnClickListener()
    {

        @Override
        public void onClick(View arg0) 
        {
            stop();
            myView.setImageResource(0);
        }
    });
}






public void play()
{
    myView  = (ImageView) findViewById(R.id.p1);

    if (audioPlayer == null)
        audioPlayer = new MediaPlayer();
    String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/warning.mp3";
    try{
        audioPlayer.setDataSource(path);
        audioPlayer.prepare();
        audioPlayer.start();

    }
    catch (IllegalArgumentException e){

        e.printStackTrace();

    }
    catch (IllegalStateException e){

        e.printStackTrace();
    }
    catch (IOException e){
        e.printStackTrace();
    }
    isPlaying = true;
}

public void again()
{
    if(stopBtn != null)
    {
        play();
    }
}

public void stop()
{
    if(audioPlayer != null)
    {
        audioPlayer.stop();
        audioPlayer.release();
        audioPlayer = null;

    }

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}






@Override
public void run() {
    // TODO Auto-generated method stub
    while(true)
    {
        try{
            br_in = br.readLine();
        }

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

}

}
}

1个回答

qq983742902
qq983742902   2013.12.31 15:22
已采纳

Socket socket = new Socket (Host,Port);

    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    msg = br.readLine();

这部分属于网络连接,不能放在主线程里操作,要新开线程结合Handler、Message来做

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!