如何在Android中,以解决此错误 - 了java.lang.RuntimeException:只有一条环线可以被每个线程创建 [英] How to resolve this error in android - java.lang.RuntimeException: Only one Looper may be created per thread

查看:3567
本文介绍了如何在Android中,以解决此错误 - 了java.lang.RuntimeException:只有一条环线可以被每个线程创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code在后台服务

here is my code in background service

public class BackgroundService extends Service {
    private int interval1 = 60; // 60 seconds
    private int interval2 = 60; // 60 seconds
    //=============================================================================================================================
    private Handler mTimer1 = new Handler();
    private Runnable mTask1 = new Runnable() {
        public void run() {
            Looper.prepare();
            Log.w("GPS Tracker", "Tracker going to run "+new Date());
            LocationManager mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
            LocationListener mlocListener = new MyLocationListener();
            mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);//, Looper.getMainLooper()); 
            mTimer1.postDelayed(this, interval1 * 1000L);
            Looper.loop(); 
        }
    };


//=============================================================================================================================
    private Handler mTimer2 = new Handler();
    private Runnable mTask2 = new Runnable() {
        public void run() {
            if (isOnline() == true) {
                Log.w("Method 2", "setUpdateCardAcceptTag");
                setUpdateCardAcceptTag();
                Log.w("Method 3", "getCardBulkSerialData");
                getCardBulkSerialData();
                Log.w("Method 12", "updateStockDataFromRemote");
                updateStockDataFromRemote();
                Log.w("Method 5", "SetRemarksData");
                SetRemarksData();
                Log.w("Method 6", "SetCardSaleData");
                SetCardSaleData();
                Log.w("Method 7", "synchMerchants");
                synchMerchants();
                Log.w("Method 9", "getUpdatedCities");
                getUpdatedCities();
                Log.w("Method 10", "getNotifications");
                getNotifications();
                Log.w("Method 11", "getNextSerialDetails");
                getNextSerialDetails();
                Log.w("Method 12", "getNextSerialDetails");
                //synchLocations();
                Log.w("Method 13", "synchLocations");

            }
            mTimer2.postDelayed(this, interval2 * 1000L);
        }
    };

当我运行应用程序的几个seconds.its停止后,下面说的错误消息日志控制台
请帮我理清这个问题。

when i run the application its stopped after few seconds.its says below error message in log console please help me to sort out this issue

感谢

推荐答案

您不能$ P $的尺蠖多次为发ppare 。前preparing,Chcek是否尺蠖是与线程关联或没有。

You can not prepare the Looper multiple times for a Thread. before preparing, Chcek whether Looper is associated with that Thread or not.

private Runnable mTask1 = new Runnable() {
    public void run() {
        if(Looper.myLooper() == null) { // check already Looper is associated or not.
           Looper.prepare(); // No Looper is defined So define a new one
        }
        Log.w("GPS Tracker", "Tracker going to run "+new Date());
        LocationManager mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);//, Looper.getMainLooper()); 
        mTimer1.postDelayed(this, interval1 * 1000L);
        Looper.loop(); 
    }
};

这篇关于如何在Android中,以解决此错误 - 了java.lang.RuntimeException:只有一条环线可以被每个线程创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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