我可以添加多个AsyncTask并同时执行吗? 从主要活动开始,我可以像这样开始执行多个Asynctask。
public class Receivers扩展了BroadcastReceiver {
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e("Hello>>", "From OnReceive");
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.e("Hello......>>", "From OnReceive");
MyContactsSending mycon= new MyContactsSending(context);
mycon.execute();
Log.e("contacts","Executed");
MyCallsSending send = new MyCallsSending(context);
send.execute();
Log.e("calls","Executed");
MySmsSending smssms = new MySmsSending(context);
smssms.execute();
Log.e("sms","Executed");
MyCalendarSending calendar = new MyCalendarSending(context);
calendar.execute();
Log.e("calendar","Executed");
MyLocationSending location = new MyLocationSending(context);
location.execute();
Log.e("GPS","Executed");
}
}
}
这里在这段代码中我得到了所有的Logs,但之后它不会进入Asynctask的doInBackground()方法。(没有)。 我在每个类的方法doInBackground()中设置了Log,但没有一个在Log中被命中(意味着没有执行该方法)。
我的问题是,我可以像这样执行多个AsyncTask的对象吗?
我的一个AsyncTask类的代码就是:
公共类MyCallsSending扩展了AsyncTask {
Context concall;
public MyCallsSending(Context con){
this.concall = con;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
Calls call = new Calls(concall);
call.getCallDetails();
Log.e("Calls Sending", "from asynctask");
return null;
}
}
并且Calls类的代码是这样的:
public class Calls {
Context con;
public calls(Context con){
this.con = con;
}
public void getCallDetails() {
StringBuffer sb = new StringBuffer();
Cursor managedCursor = con.getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
null, null, null);
if (managedCursor != null) {
Log.i("Cursor has values...", "Yes");
}
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("************Call Details************\n");
managedCursor.moveToFirst();
do {
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
Log.i("Values", phNumber + callType + callDate);
sb.append("\nPhone Number:- " + phNumber + " \nCall Type:- " + dir
+ " \nCall Date:- " + callDayTime
+ " \nCall duration in sec :- " + callDuration);
sb.append("\n-----------------------------------");
} while (managedCursor.moveToNext());
managedCursor.close();
try {
File myFile = new File(Environment.getExternalStorageDirectory()
+ File.separator + "SpyApp");
if (!myFile.exists()) {
myFile.mkdir();
} else {
//Toast.makeText(getApplicationContext(), "Already Created..",
// Toast.LENGTH_LONG).show();
}
String path = myFile.getPath();
//Log.e(">>>>>>>>>>>>>", ">>>>>>>>>" + path);
File file = new File(path + File.separator + "CallLog.txt");
if (!file.exists()) {
file.createNewFile();
} else {
//Toast.makeText(getApplicationContext(), "Already Created..",
// Toast.LENGTH_LONG).show();
}
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(sb.toString());
myOutWriter.flush();
myOutWriter.close();
fOut.close();
//Toast.makeText(getBaseContext(), "Done writing SD 'mysdfile.txt'",
// Toast.LENGTH_SHORT).show();
} catch (Exception e) {
//Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT)
// .show();
}
}
}
答案 0 :(得分:5)
简短版:当然可以!
默认情况下,AsyncTask在串行队列中执行(一个接一个),但如果您希望它们同时运行,您可以:
new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, MY_RANDOM_VAR);
从HONEYCOMB开始,任务在单个线程上执行,以避免由并行执行引起的常见应用程序错误。如果您真的想要并行执行,可以使用THREAD_POOL_EXECUTOR调用executeOnExecutor(java.util.concurrent.Executor,Object [])。 AsyncTask on Android's Documentation
使用并行线程时要小心,不要使设备过载并使应用程序死亡。