Java 同时运行多个Asyntask
我在服务器端有服务器-客户端通信程序,我有摄像头,当客户端工作时,我的android应用程序正在控制摄像头,但问题是当服务器内存满时,服务器停止摄像头并向客户端发送消息,如果客户端想自行停止摄像头,则客户端正在发送消息命令服务器和服务器停止摄像机。 问题是我没有得到按摩,如果我得到的是“内存已满”的按摩,那么当用户想要自己杀人时,我没有得到“停止摄像头”的按摩,如果我设法得到“停止摄像头”的消息,那么我没有得到“内存已满按摩” 这是我的密码,请帮帮我 提前谢谢Java 同时运行多个Asyntask,java,android,Java,Android,我在服务器端有服务器-客户端通信程序,我有摄像头,当客户端工作时,我的android应用程序正在控制摄像头,但问题是当服务器内存满时,服务器停止摄像头并向客户端发送消息,如果客户端想自行停止摄像头,则客户端正在发送消息命令服务器和服务器停止摄像机。 问题是我没有得到按摩,如果我得到的是“内存已满”的按摩,那么当用户想要自己杀人时,我没有得到“停止摄像头”的按摩,如果我设法得到“停止摄像头”的消息,那么我没有得到“内存已满按摩” 这是我的密码,请帮帮我 提前谢谢 /** here is the r
/** here is the recording start button I am calling a asyntask for recording
* Record and store video at battery control unit(server end) at background
*/
record=(ImageButton)findViewById(R.id.record);
record.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(socket==null){
Toast.makeText(getApplicationContext(), "connection not establised", Toast.LENGTH_SHORT).show();
}
else{
pDialog.show();
suspended=false;
start=false;
new CommunicationTaskrec().execute();
}
}
});
/**
*在后台运行的记录的异步任务。
*/
public class CommunicationTaskrec extends AsyncTask<Void, Void, String>{
@Override
protected String doInBackground(Void... params) {
String str = "3";
PrintWriter out = null;
try {
out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
} catch (IOException e1) {
e1.printStackTrace();
}
out.println(str);
String resultrec="testing the UI Thread update";
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
try {
resultrec = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
//Log.d("inside bg thread", resultrec);
mHandler.obtainMessage(MESSAGE_READ, resultrec).sendToTarget();
out.flush();
// new Thread(new RecThread()).start();
// new CommunicationTaskmemorycheck().execute();
return resultrec;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
首次引入时,异步任务是在单个后台线程上串行执行的。从DONUT开始,它被更改为一个线程池,允许多个任务并行运行。从蜂巢开始,任务在单个线程上执行,以避免并行执行导致的常见应用程序错误
如果确实需要并行执行,可以调用
executeOnExecutor(java.util.concurrent.Executor,Object[])
使用线程池执行器。
首次引入时,异步任务是在单个后台线程上串行执行的。从DONUT开始,它被更改为一个线程池,允许多个任务并行运行。从蜂巢开始,任务在单个线程上执行,以避免并行执行导致的常见应用程序错误
如果确实需要并行执行,可以调用
executeOnExecutor(java.util.concurrent.Executor,Object[])
使用线程池执行器。将execute()更改为executeOnExecutor(AsynTask.ThreadpoolExecutor)将execute()更改为executeOnExecutor(AsynTask.ThreadpoolExecutor)先生,它正在工作,但在第一次它不工作时,我的意思是,如果我打开应用程序并进行录制,然后手动停止相机,则我没有收到停止相机的消息,但当我回来后,再次进行录制并再次按下停止按钮,则我得到了停止相机的按摩……实际上它正在向服务器发送停止命令,服务器正在停止摄像头,但我第一次没有收到服务器的消息…您是否一直收到摄像头停止事件?没有,但当我按下对话框的停止按钮时,它将命令发送给服务器,服务器停止摄像头,但我第一次没有收到此消息正在工作,但在第一次它不工作,我的意思是,如果我打开应用程序并进行录制,然后手动停止相机,则我没有收到停止相机的消息,但当我回来后,再次进行录制并再次按下停止按钮,则我得到停止相机的按摩…实际上是这样的向服务器发送停止命令时,服务器正在停止摄像头,但我第一次没有收到服务器的消息…您是否一直收到摄像头停止事件?没有,但当我按下对话框的停止按钮时,它会向服务器发送停止摄像头的命令,但我第一次没有收到此消息
/**
* Creating a dialog box which shows a timer for the recording time
*/
case MESSAGE_READ:
final String readBuf = (String) msg.obj;
String string1 ="no enough space left on device";
if(readBuf !=null){
if(readBuf.equalsIgnoreCase(string1))
{
pDialog.dismiss();
Toast.makeText(MainActivity.this, "NO Enough Space Left for Recording Please Remove some files at server end.", Toast.LENGTH_LONG).show();
}
else{
pDialog.dismiss();
Toast.makeText(MainActivity.this, readBuf, Toast.LENGTH_LONG).show();
//here is the dialog box where I have a stop button also by which user stopping the ///camera manually
View viewlist=MainActivity.this.getLayoutInflater().inflate(R.layout.timer, null);
dialog = new Dialog(MainActivity.this);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
dialog.setContentView(viewlist);
dialog.setTitle("Status.....");
dialog.setCancelable(false);
TextView text = (TextView) dialog.findViewById(R.id.valuerec);
text.setText("Want to stop the Recording ?");
TextView cur_val = (TextView) dialog.findViewById(R.id.curvaluerec);
cur_val.setText("Recording Duration..");
Button stop = (Button) dialog.findViewById(R.id.start);
mChronometer = (Chronometer) dialog.findViewById(R.id.chronometer);
mChronometer.start();
dialog.show();
//here a asyntask and it is used for getting the memory full message
//it run contentiously on background and when the memory is full it //recieve "memory full massage"
task = new AsyncTask<Void, Void, Void>() {
String result=null;
protected Void doInBackground(Void... params) {
Runnable action = new Runnable() {
public void run() {
mHandler.obtainMessage(MESSAGE_READcreate, result).sendToTarget();
}
};
try {
do {
//Pause work if control is paused.
//tControl.waitIfPaused();
//Stop work if control is cancelled.
if (tControl.isCancelled()) {
suspended=true;
break;
}
while(!suspended){
String string1 ="memory full";
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
try {
result = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if((result!=null && result.equalsIgnoreCase(string1)) )
{
mHandler.obtainMessage(MESSAGE_READcreate, result).sendToTarget();
result=null;
suspended=true;
start=true;
runOnUiThread(action);
break;
}
}
}while (!suspended);
} catch (Exception e) {
}
return null;
}
};
task.execute();
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
task.cancel(true);
tControl.cancel();
suspended=true;
dialog.dismiss();
dialog=null;
mChronometer.stop();
pDialog.show();
//this is the runnable thread where I am getting "stopping //camera "massage
new Thread(new Runnable() {
@SuppressLint("ShowToast")
public void run() {
while (true) {
String str = "8";
PrintWriter out = null;
try {
out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
} catch (IOException e) {
e.printStackTrace();
}
out.println(str);
String resultcap=null;
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
try {
resultcap = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
mHandler.obtainMessage(MESSAGE_READstoprunning, resultcap).sendToTarget();
out.flush();
//suspended=false;
break;
}
}
}
).start();
}
});
}
}
else{
pDialog.dismiss();
Toast.makeText(MainActivity.this, "error server not respondingrec", Toast.LENGTH_LONG).show();
}
break;