Android 服务不是并行运行的
我创建了两个不同的服务(清单中列出的不同),其中包含两个不同的AsyncTask。我从两个不同的片段(相同的活动)运行它们 但如果我同时运行它们,一个服务就会排队,直到另一个服务完成。当我将这些AsyncTask直接放在片段中时(不使用服务),我可以并行运行它们 我是否可以使用服务并行运行它们 谢谢 每个服务类别的代码类似于:Android 服务不是并行运行的,android,android-asynctask,android-service,Android,Android Asynctask,Android Service,我创建了两个不同的服务(清单中列出的不同),其中包含两个不同的AsyncTask。我从两个不同的片段(相同的活动)运行它们 但如果我同时运行它们,一个服务就会排队,直到另一个服务完成。当我将这些AsyncTask直接放在片段中时(不使用服务),我可以并行运行它们 我是否可以使用服务并行运行它们 谢谢 每个服务类别的代码类似于: public class MyServiceClass extends Service{ public boolean isCancelled = false;
public class MyServiceClass extends Service{
public boolean isCancelled = false;
DownloadFile downloadFile;
@Override
public int onStartCommand(Intent intent, int flags, int startId){
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.STOP");
registerReceiver(receiver, filter);
downloadFile = new DownloadFile();
downloadFile.execute();
return 0;
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("com.example.STOP")){
isCancelled = true;
}
}
};
@Override
public void onDestroy() {
unregisterReceiver(receiver);
if (downloadFile != null && downloadFile.getStatus() != AsyncTask.Status.FINISHED){
downloadFile.cancel(true);
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private class DownloadFile extends AsyncTask<Void, String, String>
{
NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
int mId = 2;
Context context;
@Override
protected void onPreExecute() {
context = getApplicationContext();
if(context!=null){
Intent newintent = new Intent();
newintent.setAction("com.example.STOP");
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, newintent, 0);
mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle("Cover Download")
.setContentText("In progress")
.setContentIntent(pIntent)
.setSmallIcon(R.drawable.ic_launcher);
}
}
@Override
protected String doInBackground(Void... args) {
DOING TASKS
}
@Override
protected void onProgressUpdate(String... values)
{
Toast.makeText(context, values[0], Toast.LENGTH_LONG).show();
}
@Override
protected void onPostExecute(String result1)
{
mBuilder.setContentText(result1).setProgress(0,0,false);
mNotifyManager.notify(mId, mBuilder.build());
context = null;
MyServiceClass.this.stopSelf();
}
}
}
服务在UI线程中运行,因此操作系统将决定哪个服务先运行以及何时停止。如果我是你,我会为IntentService更改你的服务类,因为它们有自己的工作线程。我认为这将有助于让它们同时运行。谢谢您的评论…我将服务更改为IntentService类。。但我不明白当前onDestroy方法的代码应该放在哪里。如果您需要帮助…首先,由于您的IntentService将自动停止,您可以删除对
MyServiceClass.this.stopSelf()的调用。
。其次,如果需要检查新的意图参数,我建议您实现onHandleIntent
。最后,我不明白你为什么要更改你的onDestroy
代码,是不是没有接到电话?(很抱歉,今天的互联网太糟糕了,所以耽搁了)。。无论如何,是的,的确。。onDestroy在(异步任务的)后期执行之前被调用!我更新了我的帖子,给出了我最后的代码和日志,如果你愿意检查的话。。它也不一致(有时在PREEXECUTE!!)我看到的问题是,您在Intentservice中启动了一个异步任务,当您创建一个异步任务时,您正在应用程序中创建另一个线程,我的建议是删除Asynctask,因为IntentService可以处理关闭进程的新意图,而DoInBackground
代码可以在IntentService
内部执行,因为它有自己的工作线程。
public class MyImportServiceClass extends IntentService{
public MyImportServiceClass() {
super("MyImportServiceClass");
}
RemoteConnectivity importExport;
String code;
@Override
protected void onHandleIntent(Intent intent) {
Log.d("CHECKPOINT","INTENT START");
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.MyImportServiceClass.STOPIMEX");
String data = intent.getStringExtra("data");
code = intent.getStringExtra("code");
registerReceiver(imreceiver, filter); // TO CANCEL ASYNCTASK WHEN USER CLICK ON NOTIFICATION
importExport = new RemoteConnectivity();
importExport.execute(code,data);
}
private final BroadcastReceiver imreceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("com.example.MyImportServiceClass.STOPIMEX")){
if (importExport != null && importExport.getStatus() != AsyncTask.Status.FINISHED){
importExport.cancel(true);
}
}
}
};
@Override
public void onDestroy() {
Log.d("CHECKPOINT","DESTROY");
unregisterReceiver(imreceiver);
if(code.equals("1") || code.equals("2") || code.equals("6")){
Log.d("CHECKPOINT","DESTROY ANNOUNCE TO MAIN THREAD");
Intent intent = new Intent();
intent.setAction("com.example.MyImportServiceClass.UPDATE");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
private class RemoteConnectivity extends AsyncTask<String, String, String>
{
NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
int mId;
Context context;
@Override
protected void onPreExecute() {
Log.d("CHECKPOINT","PREEXECUTE");
context = getApplicationContext();
Intent newintent = new Intent();
newintent.setAction("com.example.MyImportServiceClass.STOPIMEX");
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, newintent, 0);
mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle("Import/Export")
.setContentText("In progress")
.setContentIntent(pIntent)
.setSmallIcon(R.drawable.ic_launcher);
}
}
@Override
protected String doInBackground(String... args) {
Log.d("CHECKPOINT","START BACKGROUND");
.............
.............
Log.d("FLAGPOINT","IMPORTING");
mylibmandbhandler db = new mylibmandbhandler(context);
while ((line = br.readLine()) != null) {
//DOING THINGS
if(isCancelled){
db.close();
return null;
}
}
db.close();
return "done";
}
@Override
protected void onPostExecute(String result1)
{
Log.d("CHECKPOINT","COMPLETED");
}
@Override
protected void onCancelled(){
Log.d("CHECKPOINT","CANCELLED");
}
}
}
01-03 06:49:36.367: D/CHECKPOINT(2019): INTENT START
01-03 06:49:36.387: D/CHECKPOINT(2019): PREEXECUTE
01-03 06:49:36.397: D/CHECKPOINT(2019): START BACKGROUND
01-03 06:49:36.457: D/CHECKPOINT(2019): DESTROY
01-03 06:49:36.457: D/CHECKPOINT(2019): ANNOUNCE
01-03 06:49:36.997: D/FLAGPOINT(2019): IMPORTING