是否有可能使用的AsyncTask在服务类? [英] Is it possible to use AsyncTask in a Service class?

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

问题描述

一切都在标题。

在更说明需要注意的是服务,如其他应用程序对象,在其宿主进程的主线程中运行和AsyncTask的只有当它在执行公务单证在UIThread。

那么,有可能使用的AsyncTask在服务类?

我想这样做,但我总是得到同样的错误

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

...

  05-01 18:09:25.487:ERROR / JavaBinder(270):由:java.lang.RuntimeException的:无法创建处理程序中的线程并没有所谓的活套。 prepare()
 

我是不是做错了什么,或者这只是不可能?

下面是我的服务类的code

 包com.eip.core;

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

公共类网络服务延伸服务{


    私人最终INetwork.Stub mBinder =新INetwork.Stub(){

        @覆盖
        公众诠释doConnect(字符串地址,INT口)将抛出RemoteException {
            新ConnectTask()执行(test42);
            返回0;
        }
    };

    @覆盖
    公众的IBinder onBind(意向为arg0){
        返回mBinder;
    }

    私有类ConnectTask扩展的AsyncTask<字符串,太虚,太虚> {

        @覆盖
        在preExecute保护无效(){
            super.on preExecute();
            Log.i(开preExecute(),);
        }

        @覆盖
        保护无效doInBackground(字符串...为arg0){
            Log.i(doInBackground(),);
            返回null;
        }

        @覆盖
        保护无效onPostExecute(无效的结果){
            super.onPostExecute(结果);
            Log.i(OnPostExecute(),);
        }

    }
}
 

解决方案

我想我找到为什么它不工作在我的情况。

在这里,我使用这样的:

 私人最终INetwork.Stub mBinder =新INetwork.Stub(){

        @覆盖
        公众诠释doConnect(字符串地址,INT口)将抛出RemoteException {
            新ConnectTask()执行(test42);
            返回0;
        }
    };
 

我用这做什么所谓的IPC,进程间通信,所以我想,我的服务,我的活动有两种型动物的过程中,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.

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

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