Monday, February 24, 2014

Between Wifi Android phone via a communication problem with the PC (with code)


This a mobile client program, the server program on the PC. Mobile phones and computers are in the same LAN, the problem now is I pressed the send button on a PC does not show. Great God, please help
package com.example.send;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {



private Button MyButton = null;
private Button MyExitButton = null;
private TextView MyTextView = null;
private EditText MyEditText = null;

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
MyButton = (Button) findViewById (R.id.mybutton);
MyTextView = (TextView) findViewById (R.id.mytext);
MyEditText = (EditText) findViewById (R.id.myedit);
MyExitButton = (Button) findViewById (R.id.exitbutton);
MyTextView.setText (R.string.Text);
MyButton.setText (R.string.button);
MyExitButton.setText (R.string.exitbutton);
MyButton.setOnClickListener (new MyButtonOnClickListener ());
MyExitButton.setOnClickListener (new MyExitButtonOnClickListener ());

Resources res = getResources () ;/ / set back color
ActivityDrawable drawable = res.getDrawable (R.drawable.bkcolor);
this.getWindow () setBackgroundDrawable (drawable);.


}

class MyButtonOnClickListener implements OnClickListener {

@ Override
public void onClick (View v) {
/ / TODO Auto-generated method stub
new ClientThread () start ();.
}
}

class MyExitButtonOnClickListener implements OnClickListener {

@ Override
public void onClick (View v) {
/ / TODO Auto-generated method stub
finish ();
}
}

class ClientThread extends Thread {
public void run ()
{
try
{
/ / WifiManager wifiManager = (WifiManager) getSystemService (WIFI_SERVICE);
/ / WifiManager.startScan ();
Socket socket = new Socket ("192.168.1.251", 10000);
OutputStream outputstream = socket.getOutputStream ();
String str = "hello";
byte buffer [] = str.getBytes ();
outputstream.write (buffer);
outputstream.flush ();
socket.close ();

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

}
}<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
So nobody cares even score ~

No comments:

Post a Comment