Java 如何为show运行Asynctask;“数据正在读取”;运行线程中的对话框
我有一个持续运行的线程,当我发送命令获取数据时,数据开始出现。我想向Asynctask演示这个过程,让用户能够理解正在读取的数据。我怎样才能做到这一点请帮助我。现在我用的是定时器。但计时器将在固定时间内工作。但如果我的数据更少,那个时间,计时器将占用我不想要的时间。如果有人有任何想法,请让我知道。。。。提前谢谢你的帮助Java 如何为show运行Asynctask;“数据正在读取”;运行线程中的对话框,java,android,multithreading,Java,Android,Multithreading,我有一个持续运行的线程,当我发送命令获取数据时,数据开始出现。我想向Asynctask演示这个过程,让用户能够理解正在读取的数据。我怎样才能做到这一点请帮助我。现在我用的是定时器。但计时器将在固定时间内工作。但如果我的数据更少,那个时间,计时器将占用我不想要的时间。如果有人有任何想法,请让我知道。。。。提前谢谢你的帮助 I had use this but because of running thread message getting continuously.
I had use this but because of running thread message getting continuously.
public static class Test extends AsyncTask<String, String, String> {
ProgressDialog progressDialog;
DataBaseController dataBaseController;
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog( context);
progressDialog.setTitle( "Sync Data" );
progressDialog.setMessage( "Syncing..." );
progressDialog.setCancelable( false );
progressDialog.setIcon( android.R.drawable.ic_dialog_info );
progressDialog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
dataBaseController = DataBaseController.getnstance( context );
// seperating the data which is getting from getResponse method
if (strings[0] != null) {
String ok = strings[0].replace( "OK", "" ).replace( "Scale id,Rec.No,Date,Time,Bill No.,Item No.,Plu,Name,Qty,Rate,Amount,Void", "" );
String[] data = ok.split( "," );
Log.i( "TEST", strings[0] );
for (int i = 0; i < data.length; i++) {
char c = strings[0].charAt( i );
if (c == ',') {
int id = Integer.parseInt( data[0] );
int rec_no = Integer.parseInt( (data[1]) );
String Date = data[2];
if (Date != null)
Date = custom_date_format( Date );
String Time = data[3];
int Bill_No = Integer.parseInt( data[4] );
int Item_No = Integer.parseInt( data[5] );
int Plu = Integer.parseInt( data[6] );
String Name = data[7];
String weight_type = data[8];
int Unit;
if (weight_type=="Kg"){
Unit=0;
}else{
Unit=1;
}
float Qty = Float.parseFloat( data[9] );
float Rate = Float.parseFloat( data[10] );
float Amount = Float.parseFloat( data[11] );
String Void = data[12];
PLU plu = new PLU( id, rec_no, Date, Time, Bill_No, Item_No, Plu, Name, Unit, Qty, Rate, Amount );
boolean check = dataBaseController.isBillExist( plu.getRec_no() );
if (!check) {
dataBaseController.Trasaction(plu);
}
}
}
}
return String.valueOf( strings );
}
@Override
protected void onPostExecute(String result) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (result != null) {
Toast.makeText( context, "Mission successfully", Toast.LENGTH_SHORT ).show();
}else{
Toast.makeText( context, "Mission failed", Toast.LENGTH_SHORT ).show();
}
}
}
*******Thread which is connected*****************
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
boolean get_actualdata = false;
private boolean stop = false;
private boolean hasReadAnything = false;
public ConnectedThread(BluetoothSocket socket) {
Log.d( TAG, "create ConnectedThread" );
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
// tmpIn.skip( 22 );
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e( TAG, "temp sockets not created", e );
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void shutdown() {
stop = true;
if (!hasReadAnything) return;
if (mmInStream != null) {
try {
mmInStream.close();
} catch (IOException e) {
Log.e( TAG, "close() of InputStream failed." );
}
}
}
public void run() {
boolean send_validation = true;
int version;
while (true) {
try {
version = readfunction(); // reading the version response after sending command on bluetooth
} catch (Exception e) {
e.printStackTrace();
connectionLost();
break;
}
if (version == 0) {
if (send_validation && !get_actualdata) {
StringParsing( str.toString() );// it will seperate the version
get_actualdata = true;
}
} else {
try {
readfunction();
} catch (IOException e) {
e.printStackTrace();
}
}
if (get_actualdata && send_validation) {
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
int ok = Handle_OK();//if getting ok
String status;
if (ok == 1) {
status = "OK";
mHandler.readLine( status );
} else {
status = "Not Recognised with device";
mHandler.readLine( status );
}
getReportResponse();
}
} catch (IOException e) {
connectionLost();
e.printStackTrace();
break;
}
}
}
}
//this method calling in running thread , it will response when command send by click on button
public void getReportResponse() throws IOException {
boolean with_header = false;
try {
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( mmInStream ) );
String line = null;
while ((line = bufferedReader.readLine()) != null) {
// String finalLine = line;
if (reset_the_reader_flag) { //if reset_the_header will true
with_header = false;
}
String f_line = custom_filter(line);
if (!f_line.isEmpty()){
if (with_header) { // if with_header flag will false then it will jump in else
// dataparse.ReportData( f_line);
new Handler( Looper.getMainLooper()).post( new Runnable() {
@Override
public void run() {
//Asynctask when command send this method will run
TabularFragment.Test data = new TabularFragment.Test();
data.execute( f_line );
}
} );
} else {
// if header matches set the with_header flag true, then exist from else part and again check with_heaer flag
// if header will not match, will jump in else and read the data that is without header.
if (f_line.matches( "Scale(.*)" )) {
with_header = true;
} else {//
dataparse.Data( f_line );
}
reset_the_reader_flag = false;
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
mmInStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我曾经使用过这个,但是因为运行线程消息不断地获取。
公共静态类测试扩展了异步任务{
进行对话进行对话;
数据库控制器;
@凌驾
受保护的void onPreExecute(){
progressDialog=新建progressDialog(上下文);
progressDialog.setTitle(“同步数据”);
progressDialog.setMessage(“同步…”);
progressDialog.setCancelable(假);
progressDialog.setIcon(android.R.drawable.ic_dialog_info);
progressDialog.show();
super.onPreExecute();
}
@凌驾
受保护的字符串背景(字符串…字符串){
dataBaseController=dataBaseController.GetInstance(上下文);
//分离从getResponse方法获取的数据
if(字符串[0]!=null){
字符串ok=strings[0]。替换(“ok”和“”)。替换(“秤号、收款号、日期、时间、票据号、物料号、Plu、名称、数量、费率、金额、作废”和“”);
String[]data=ok.split(“,”);
Log.i(“测试”,字符串[0]);
对于(int i=0;i