是否可以在 Service 类中使用 AsyncTask? [英] Is it possible to use AsyncTask in a Service class?

查看:31
本文介绍了是否可以在 Service 类中使用 AsyncTask?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一切都在标题中.

官方文档说注意,服务和其他应用程序对象一样,运行在其宿主进程的主线程中,并且AsyncTask只有在UIThread中执行时才有效.>

那么可以在Service类中使用AsyncTask吗?

我正在尝试这样做,但总是遇到相同的错误

05-01 18:09:25.487: 错误/JavaBinder(270): java.lang.ExceptionInInitializerError

...

05-01 18:09:25.487: ERROR/JavaBinder(270): 引起: java.lang.RuntimeException: 无法在没有调用 Looper.prepare() 的线程内创建处理程序

我做错了什么还是根本不可能?

这是我的服务类的代码

package com.eip.core;导入 android.app.Service;导入 android.content.Intent;导入 android.os.AsyncTask;导入 android.os.IBinder;导入 android.os.RemoteException;导入 android.util.Log;公共类 NetworkService 扩展服务 {私有最终 INetwork.Stub mBinder = new INetwork.Stub() {@覆盖public int doConnect(String addr, int port) 抛出 RemoteException {new ConnectTask().execute("test42");返回0;}};@覆盖公共IBinder onBind(意图arg0){返回 mBinder;}私有类 ConnectTask 扩展了 AsyncTask{@覆盖受保护的无效 onPreExecute() {super.onPreExecute();Log.i("OnPreExecute()", "");}@覆盖受保护的 Void doInBackground(String... arg0) {Log.i("doInBackground()", "");返回空;}@覆盖protected void onPostExecute(Void 结果) {super.onPostExecute(result);Log.i("OnPostExecute()", "");}}}

解决方案

我想我找到了为什么它在我的情况下不起作用.

我在这里使用这个:

private final INetwork.Stub mBinder = new INetwork.Stub() {@覆盖public int doConnect(String addr, int port) 抛出 RemoteException {new ConnectTask().execute("test42");返回0;}};

我用它来做所谓的IPC,进程间通信,所以我猜我的Service和我的Activity在两个不同的进程中,AsyncTask必须在主UI线程中执行到 android 文档,所以根据这些事实,我为什么要尝试这样做在我看来是不可能的.

如果我错了,请有人纠正我.

Everything is in the title.

On the official documentations it is stated that Note that services, like other application objects, run in the main thread of their hosting process and AsyncTask only works if it is executed in the UIThread.

So is it possible to use AsyncTask in a Service class?

I am trying to do so but I'm always getting the same error

05-01 18:09:25.487: ERROR/JavaBinder(270): java.lang.ExceptionInInitializerError

...

05-01 18:09:25.487: ERROR/JavaBinder(270): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

Am I doing something wrong or is this just impossible ?

Here is the code of my Service class

package com.eip.core;

import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class NetworkService extends Service {


    private final INetwork.Stub mBinder = new INetwork.Stub() {

        @Override
        public int doConnect(String addr, int port) throws RemoteException {
            new ConnectTask().execute("test42");
            return 0;
        }
    };

    @Override
    public IBinder onBind(Intent arg0) {
        return mBinder;
    }

    private class ConnectTask extends AsyncTask<String, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Log.i("OnPreExecute()", "");
        }

        @Override
        protected Void doInBackground(String... arg0) {
            Log.i("doInBackground()", "");
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            Log.i("OnPostExecute()", "");
        }

    }
}

解决方案

I Think I found why it is not working in my case.

Here I am using this :

private final INetwork.Stub mBinder = new INetwork.Stub() {

        @Override
        public int doConnect(String addr, int port) throws RemoteException {
            new ConnectTask().execute("test42");
            return 0;
        }
    };

I am using this to do what so called IPC, Inter Process Communication, so I guess that my Service and my Activity are in two differents process, AsyncTask must be executed in the main UI thread according to the android doc, so why I was trying to do seems to me just impossible according to those facts.

If I am wrong please someone can correct me.

这篇关于是否可以在 Service 类中使用 AsyncTask?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆