通过套接字接收文件时关闭警报对话框生成器
Close Alert Dialog Builder when File is Received via Socket
我有一个使用套接字的 PC(客户端)到 Android(服务器)文件传输功能。
在服务器端打开连接时,我会触发一个警报对话框以通过单击按钮指示连接。现在我需要的是当收到文件时,警告对话框消息会自动关闭。
这是我的按钮功能
POButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final AlertDialog.Builder dele = new AlertDialog.Builder(getActivity());
dele.create();
dele.setCancelable(false);
dele.setTitle("Waiting for File. . .");
final Socket[] socket = new Socket[1];
final ServerSocket[] server = new ServerSocket[1];
final BufferedReader[] br = new BufferedReader[1];
final PrintWriter[] pw = new PrintWriter[1];
final String ip = getIpAddress(getContext());
dele.setMessage("IP Address:\n" + ip);
new Thread(new Runnable() {
@Override
public void run(){
try {
while(true){
server[0] = new ServerSocket(0);
server[0].close();
server[0] = new ServerSocket(8998);
socket[0] = server[0].accept();
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
String curDate = dateFormat.format(date);
File sdcard = Environment.getExternalStorageDirectory();
File myFile = new File(sdcard,"TestReceived"+curDate+".csv");
br[0] = new BufferedReader(new InputStreamReader(socket[0].getInputStream()));
pw[0] = new PrintWriter(new FileWriter(myFile));
try {
String line;
for (line = br[0].readLine(); line != null; line = br[0].readLine()){
pw[0].println(line);
}
pw[0].flush();
pw[0].close();
}
catch (Exception e){
System.out.printf("Can't write to file. ", e);
}
finally {
new toastview().toast("IP Address:\n"+ ip + curDate, getActivity()).show();
if (socket[0] != null){
br[0].close();
pw[0].close();
server[0].close();
}
}
}
}
catch (Exception e){
e.printStackTrace();
}
}
}).start();
dele.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (socket[0] != null){
try {
br[0].close();
pw[0].close();
server[0].close();
} catch (IOException e) {
e.printStackTrace();
}
}
dialogInterface.dismiss();
}
});
dele.show();
}
});
return inflate;
}
我已经尝试过这些功能,但还是不行。
dele.dismiss();
dele.cancel();
dialogInterface.dismiss();
只是显示错误 "Symbol not Found"。
dele
是 AlertDialog.Builder
的实例。调用 .show()
时抓住对话框,然后关闭它。
我有一个使用套接字的 PC(客户端)到 Android(服务器)文件传输功能。 在服务器端打开连接时,我会触发一个警报对话框以通过单击按钮指示连接。现在我需要的是当收到文件时,警告对话框消息会自动关闭。
这是我的按钮功能
POButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final AlertDialog.Builder dele = new AlertDialog.Builder(getActivity());
dele.create();
dele.setCancelable(false);
dele.setTitle("Waiting for File. . .");
final Socket[] socket = new Socket[1];
final ServerSocket[] server = new ServerSocket[1];
final BufferedReader[] br = new BufferedReader[1];
final PrintWriter[] pw = new PrintWriter[1];
final String ip = getIpAddress(getContext());
dele.setMessage("IP Address:\n" + ip);
new Thread(new Runnable() {
@Override
public void run(){
try {
while(true){
server[0] = new ServerSocket(0);
server[0].close();
server[0] = new ServerSocket(8998);
socket[0] = server[0].accept();
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
String curDate = dateFormat.format(date);
File sdcard = Environment.getExternalStorageDirectory();
File myFile = new File(sdcard,"TestReceived"+curDate+".csv");
br[0] = new BufferedReader(new InputStreamReader(socket[0].getInputStream()));
pw[0] = new PrintWriter(new FileWriter(myFile));
try {
String line;
for (line = br[0].readLine(); line != null; line = br[0].readLine()){
pw[0].println(line);
}
pw[0].flush();
pw[0].close();
}
catch (Exception e){
System.out.printf("Can't write to file. ", e);
}
finally {
new toastview().toast("IP Address:\n"+ ip + curDate, getActivity()).show();
if (socket[0] != null){
br[0].close();
pw[0].close();
server[0].close();
}
}
}
}
catch (Exception e){
e.printStackTrace();
}
}
}).start();
dele.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (socket[0] != null){
try {
br[0].close();
pw[0].close();
server[0].close();
} catch (IOException e) {
e.printStackTrace();
}
}
dialogInterface.dismiss();
}
});
dele.show();
}
});
return inflate;
}
我已经尝试过这些功能,但还是不行。
dele.dismiss();
dele.cancel();
dialogInterface.dismiss();
只是显示错误 "Symbol not Found"。
dele
是 AlertDialog.Builder
的实例。调用 .show()
时抓住对话框,然后关闭它。