无法创建内螺纹处理程序。我如何使用活套。prepare()? [英] Can't create handler inside thread. How do I use Looper.prepare()?

查看:111
本文介绍了无法创建内螺纹处理程序。我如何使用活套。prepare()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/3614663/cant-create-handler-inside-thread-that-has-not-called-looper-$p$ppare-inside-a\">Can’t里面活套。prepare()线程已经不叫里面的AsyncTask的ProgressDialog 创建处理程序

我开发一个Android的服务,它试图获取设备的IP地址每x的时间,它comunicate到服务器。
我使用的是:

I'm developing an Android service that try to obtain the device IP address every x time and comunicate it to a server. I'm using:

Netbeans的7.2
Android SDK中
谷歌Android的API 8
的SQLite

Netbeans 7.2
Android SDK
Android Google-Api 8
SQLite

我知道有与此相同的问题了几个问题,但他们都不给我解决我的问题。正如你可以在我的下面code看到的,我不会试图访问该服务主线程的UI(好吧,我试过,但我评论了行之后,误差保持不变)。在另一方面,我使用的的AsyncTask 的,我认为这是适当的方式来做到这一点。

I know there are a few questions related to this same issue, but none of them gave me a solution to my problem. As you can see in my code below, I'm not trying to access to the UI of the service main thread (well, I tried, but after I commented the line, the error remains the same). On the other hand, I'm using AsyncTask, which I think is the appropriate way to do it.

这是我服务的主要部分:

This is the main part of my service:

public class ArsosService extends Service {

    private NotificationManager mNM;
        private final Messenger mMessenger = new Messenger(new IncomingHandler());
        protected DatabaseUtil dbu = null;
        @Override
        public void onCreate() {
            mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            try {
                dbu = DatabaseUtility.getInstance(this);
            } catch (IOException ex) {
                Log.e("Service", ex);
            }
            Timer timer = new Timer();
            timer.schedule(new Checks(), 0, 15000);
        }

        private class Checks extends TimerTask {
            @Override
            public void run() {
                CheckIpAddress_Task checkIp = new CheckIpAddress_Task();         
                checkIp.execute();
            }
        }

        // Other methods

        private class CheckIpAddress_Task extends AsyncTask<Void, Void, Integer> {
        @Override
        protected Integer doInBackground(Void... arg0) {
            String ipLocal = getLocalIpAddress();
            String text = null;

            // ipLocal==null means there is no available connection, so we do nothing. 
            if (ipLocal != null) {
                String ipDb = dbu.getLastIP(); // we get the IP saved in the DB.
                if (ipDb == null) {
                    dbu.addProperty("IP", ipLocal); // we save the IP in the DB.
                } else if (!ipLocal.equals(ipDb)) {
                    dbu.setProperty("IP", ipLocal); // we update the IP in the DB.
                }
            }
            if (text != null) {
                //showNotification(1, text, ipLocal);
            }
            return 0;
        }

        private String getLocalIpAddress() {
            String result = null;
            // Irrelevant code
            return result;
        }
    }
}

我认为这个问题可能与线程,但我看不到的地方。任何帮助将AP preciated。

I think the problem may be related to the threads, but I can't see where. Any help will be appreciated.

编辑:虽然我已经接受了其中一个答案为正确,或者因为它,我一直在寻找一些信息关于它。我碰到此页面我想和大家谁哪天需要更多地了解这个问题分享。它的作者,Tejas的Lagvankar,介绍活动主题,尺蠖和处理在一个非常清晰易懂的方式应有尽有。

EDITED: Although I have accepted one of the answers as correct, or maybe because of it, I've been searching for some more information regard to it. I've run into this page I want to share with all of you who someday need to know more about this issue. Its author,Tejas Lagvankar, explains everything about threads, loopers and handler in a very clear and understandable way.

推荐答案

试试这个...

- 首先声明处理程序对象的引用变量的类范围

- First declare the Handler Object reference variable at class scope.

处理程序H;

- 的onCreate()方法创建的处理程序

- Inside the onCreate() method create the instance of the Handler.

H =新的处理程序();

- 螺纹使用它象下面这样:

- Use it with thread like below:

new Thread(new Runnable(){

 public void run(){

    h.post(new Runnable(){


      // Do the UI work here.

   });

 }


});

- 您可以很好地使用的AsyncTask ,在android系统提供的,它为P知*的 ainless线程 *

- You can very well use the AsyncTask, provided in android, its known as P*ainless threading.*

这篇关于无法创建内螺纹处理程序。我如何使用活套。prepare()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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