package pip.Wifi;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.LinkedList;
import java.util.List;
import pip.Bluetooth.BluetoothCS;
import pip.Bluetooth.BluetoothFile;
import pip.Tool.UnitSize;
import wyf.wpf.NewMain;
import wyf.wpf.choosecontactlocal;
import wyf.wpf.importing;
import wyf.wpf.R;
import wyf.wpf.Constant;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class WifiReceive extends Activity {
private TextView ReceiveRemind = null;// 提醒客户用的text
private RelativeLayout progresscontainer = null;// 包含进度条的layout
private ProgressBar Cyclepb = null;// 等待对方连接圆形进度
private ProgressBar Horipb = null;// 接受信息进度
private CheckTimeoutThread m_CheckTimeoutThread = null;// 用于检测服务端等待超时的线程
static List<Byte> Bl = new LinkedList<Byte>();
static FileOutputStream Rev_Fs;
private boadIpThread boadIP = null;
private String username = null;
private String MobileName = null;
private String OpponentIP = null;
private static final int CONFIRM = 11;
public static final byte CONFIRM_FLAG = 0;
public static final byte CANCEL_FLAG = 1;
private DatagramSocket datasocket = null;
private ServerSocket connectss = null;
private Socket connectsocket = null;
private ServerSocket getVcardss = null;
private Socket getVcardsocket = null;
private final Handler mHandler = new Handler() {
Bundle b = null;
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case WifiHelper.WIFICONNECT:
showDialog(CONFIRM);// 调用onCreateDialog
break;
case WifiHelper.UDP_SEND:
ReceiveRemind.setGravity(Gravity.CENTER);
ReceiveRemind.setText(getString(R.string.udp_send));
break;
case Constant.SERVICE_ON:
ReceiveRemind.setGravity(Gravity.CENTER);
ReceiveRemind.setText(getString(R.string.server_start));
break;
case Constant.MESSAGE_CONNECTED:
// 使用interrupt停止检测超时线程的运行
m_CheckTimeoutThread.interrupt();
m_CheckTimeoutThread = null;
ReceiveRemind.setGravity(Gravity.CENTER);
ReceiveRemind.setText(getString(R.string.receiving));
progresscontainer.removeView(Cyclepb);
break;
case Constant.INIT_PROG:
b = msg.getData();
int max = b.getInt("Total");
Horipb.setMax(max);
RelativeLayout.LayoutParams barlp = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, // width
ViewGroup.LayoutParams.WRAP_CONTENT // height
);
UnitSize.mysetMargins(WifiReceive.this, barlp, 0, 100, 0, 0);
progresscontainer.addView(Horipb, barlp);
Horipb.setVisibility(View.VISIBLE);
break;
case Constant.UPDATE_PROG:
b = msg.getData();
int progress = b.getInt("Progress");
Horipb.setProgress(progress);
break;
case Constant.CONNECT_TIMEOUT:
AlertDialog dlg4 = new AlertDialog.Builder(WifiReceive.this)
.setTitle("超时操作").setMessage(
"您已等待较长时间,是否返回重新操作?选择“是”返回,选择“否”继续等待。")
.setPositiveButton("是",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
onBackPressed();
}
}).setNeutralButton("否",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int witch) {
dialog.dismiss();
// 监听后运行超时检测线程
m_CheckTimeoutThread = new CheckTimeoutThread(
mHandler);
m_CheckTimeoutThread.start();
}
}).create();
dlg4.show();
break;
case Constant.MESSAGE_RECEIVED:
Horipb.setVisibility(View.INVISIBLE);
Intent i = new Intent(WifiReceive.this, importing.class);
i.putExtra("filepath", WifiHelper.WIFIRECEIVEPATH);
startActivity(i);
break;
}
}
};
/************** 提示对话框 ***************/
protected Dialog onCreateDialog(int id) {
switch (id) {
case CONFIRM:
String tempstring = "您是否确认接受对方用户的链接请求:" + MobileName;
return new AlertDialog.Builder(WifiReceive.this).setTitle("温馨提示")
.setMessage(tempstring).setIcon(
android.R.drawable.ic_dialog_info)
.setPositiveButton("确认", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SendResponseThread sp = new SendResponseThread(
CONFIRM_FLAG);
sp.start();
// 启动接收线程
ServerThread server = new ServerThread(mHandler);
server.start();
}
}).setNegativeButton("取消", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SendResponseThread sp = new SendResponseThread(
CANCEL_FLAG);
sp.start();
}
}).create();
default:
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// setContentView(R.layout.btserverfetch);
setContentView(R.layout.wifireceive);
progresscontainer = (RelativeLayout) findViewById(R.id.progresscontainer);
Horipb = (ProgressBar) findViewById(R.id.importbar);
Cyclepb = (ProgressBar) findViewById(R.id.cyclebar);
ReceiveRemind = (TextView) findViewById(R.id.btservertext);
String ss = getString(R.string.building_server);
progresscontainer.removeView(Horipb);
ReceiveRemind.setText(ss);
RelativeLayout.LayoutParams barlp = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, // width
ViewGroup.LayoutParams.WRAP_CONTENT // height
);
UnitSize.mysetMargins(WifiReceive.this, barlp, 135, 100, 0, 0);
progresscontainer.removeView(Cyclepb);
progresscontainer.addView(Cyclepb, barlp);
Cyclepb.setVisibility(View.VISIBLE);
Intent intent = getIntent();
username = intent.getStringExtra("username");
DatagramSocket ds;
try {
ds = new DatagramSocket(WifiHelper.UDP_BOADCASTPORT);
ds.setBroadcast(true);
// 创建并启动发送线程
boadIP = new boadIpThread(username, ds);
boadIP.start();
mHandler.obtainMessage(WifiHelper.UDP_SEND).sendToTarget();
} catch (SocketException e1) {
System.out.println("广播IP的Socket错了");
e1.printStackTrace();
} catch (Exception e) {
System.out.println("广播IP的Socket错了");
e.printStackTrace();
}
// 启动connect请求监听线程
GetConnectThread connect = new GetConnectThread(mHandler);
connect.start();
}
/**
* 广播自己用户名的线程
*
* @author XijieChen
*
*/
class boadIpThread extends Thread {
private String username;
public boadIpThread(String name, DatagramSocket dsocket)
throws Exception {
username = name;
datasocket = dsocket;
}
public void run() {
try {
while (true) {
// 构造发送数据报 把自己的名字发送出去
byte[] data = username.getBytes();
DatagramPacket outgoing = new DatagramPacket(data,
data.length, InetAddress
.getByName("255.255.255.255"),
WifiHelper.UDP_BOADCASTPO
同一Wifi网段匹配、传输文件
4星 · 超过85%的资源 需积分: 17 86 浏览量
2012-04-04
16:08:25
上传
评论 9
收藏 11KB RAR 举报
水牛
- 粉丝: 16
- 资源: 3
- 1
- 2
- 3
- 4
- 5
- 6
前往页