package com.redmany.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import com.redmany.Fiel.ReadFromFile;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class HgCdemoActivity extends Activity implements OnClickListener {
private String Ip = "192.168.0.105";
// private String Ip = "192.168.0.112";
private int port= 6666;
private String infodata="";
private TextView infodispaly;
private EditText infotext;
private Button infobut;
private Button intoaudio;
private Button intovideo;
private Button intofile;
private EditText ipText;
private DatagramSocket dSocket;
private DatagramSocket socket;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.penaltyLog()
.penaltyDeath()
.build());
// SendData datas = new SendData();
try {
dSocket = new DatagramSocket();
socket = new DatagramSocket(6666);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str = "123";
byte data [] = str.getBytes();
// datas.sendMsg(data);
init();
testthear test = new testthear();
new Thread(test).start();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
recorder.stop();
recorder.release();
socket.close();
dSocket.close();
}
private void init(){
infotext = (EditText) findViewById(R.id.clietin_text);
infodispaly = (TextView) findViewById(R.id.infodisplay);
infobut = (Button) findViewById(R.id.but_ok);
infobut.setOnClickListener(this);
intoaudio = (Button) findViewById(R.id.intoaudio);
intoaudio.setOnClickListener(this);
intovideo = (Button) findViewById(R.id.intovideo);
intovideo.setOnClickListener(this);
intofile = (Button) findViewById(R.id.intofile);
intofile.setOnClickListener(this);
ipText = (EditText) findViewById(R.id.ip_text);
}
private void Cliet(byte[] data){
try {
int msg_length = data.length;
System.out.println(Ip+"-----------"+new String(data).toString());
InetAddress local = InetAddress.getByName(Ip);
DatagramPacket p = new DatagramPacket(data, msg_length, local, port);
dSocket.send(p);
System.out.println("发送了");
} catch (IOException e) {
e.printStackTrace();
}
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Bundle b = msg.getData();
infodata += '\n'+b.getString("result");
infodispaly.setText(infodata);
// Toast.makeText(HgSdemoActivity.this, infodata, 3000).show();
}
};
public class testthear extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Server1();
}
}
private void Server1(){
//创建一个DatagramSocket对象,并指定监听的端口号
while (true) {
try {
byte data [] = new byte[1024];
//创建一个空的DatagramPacket对象
DatagramPacket packet = new DatagramPacket(data,data.length);
//使用receive方法接收客户端所发送的数据,
//如果客户端没有发送数据,该进程就停滞在这里
System.out.println("等待呢");
socket.receive(packet);
System.out.println("回来了");
String result = new String(packet.getData(),packet.getOffset(),packet.getLength());
// Toast.makeText(this, "datas=="+result, 3000).show();
// System.out.println("result--->" + result);
Message msg = new Message();
Bundle b = new Bundle();
b.putString("result", result);
msg.setData(b);
handler.sendMessage(msg);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.but_ok:
String str = "";
str = infotext.getText().toString();
byte data [] = str.getBytes();
Cliet(data);
break;
case R.id.intoaudio:
initializeAudio();
break;
case R.id.intovideo:
readFileByBytes("/sdcard/peipei.amr");
break;
case R.id.intofile:
break;
}
}
/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*/
public void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
// 一次读多个字节
byte[] tempbytes = new byte[1024];
int byteread = 0;
in = new FileInputStream(fileName);
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while (true) {
if((byteread = in.read(tempbytes)) == -1){
tempbytes[0] = (byte)0xff;
tempbytes[1] = (byte)0xff;
Cliet(tempbytes);
break;
}else{
Cliet(tempbytes);
System.out.write(tempbytes, 0, byteread);
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}
private MediaRecorder recorder;
private void initializeAudio() {
recorder = new MediaRecorder();// new出MediaRecorder对象
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 设置MediaRecorder的音频源为麦克风
recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
// 设置MediaRecorder录制的音频格式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// 设置MediaRecorder录制音频的编码为amr.
recorder.setOutputFile("/sdcard/peipei.amr");
// 设置录制好的音频文件保存路径
try {
recorder.prepare();// 准备录制
recorder.start();// 开始录制
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页