Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android后台服务错误:Can';t在尚未调用的线程内创建处理程序_Java_Android_Gps - Fatal编程技术网

Java Android后台服务错误:Can';t在尚未调用的线程内创建处理程序

Java Android后台服务错误:Can';t在尚未调用的线程内创建处理程序,java,android,gps,Java,Android,Gps,这是我的任务: 我需要运行2个后台服务。一个用于通过ksoap将我的移动数据同步到服务器。另一个用于将我的gps位置发送到服务器。 所以我写了这段代码。但是当我添加gpsTimer部分时,它在加载应用程序时给出了以下错误。请帮助我解决这个问题,或者告诉我实现此情况的任何其他方法 这是我的代码 public class BackgroundService extends Service { private Timer timer; private Timer gpsTimer;

这是我的任务: 我需要运行2个后台服务。一个用于通过ksoap将我的移动数据同步到服务器。另一个用于将我的gps位置发送到服务器。 所以我写了这段代码。但是当我添加gpsTimer部分时,它在加载应用程序时给出了以下错误。请帮助我解决这个问题,或者告诉我实现此情况的任何其他方法

这是我的代码

public class BackgroundService extends Service {

    private Timer timer;
    private Timer gpsTimer;

    public IBinder onBind(Intent intent) {return null;}

    public void onCreate() {
        super.onCreate();
        timer = new Timer("BackgroundServiceTimer");
        timer.schedule(syncTask, 1000L, 60 * 1000L);

        gpsTimer = new Timer("GPSServiceTimer");
        gpsTimer.schedule(syncTaskGps, 1000L, 10 * 1000L);

    }

    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
        timer = null;

        gpsTimer.cancel();
        gpsTimer = null;
    }

    public void onLowMemory() {
        super.onLowMemory();
        stopService(new Intent(com.lk.lankabell.android.activity.BackgroundService.class.getName()));

    }

    private TimerTask syncTaskGps = new TimerTask() {
        public void run() {
          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);    
        }
    };
    private TimerTask syncTask = new TimerTask() {
        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");

            }
        }
    };
这是我的错误

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

TimerTask在与UI线程不同的线程上运行,并且根据文档,如果调用线程没有循环器,则会引发
RuntimeException
。尝试以下方式更改代码:

private TimerTask syncTaskGps = new TimerTask() {
        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()); 
          Looper.loop();   
        }
    };

TimerTask在与UI线程不同的线程上运行,并且根据文档,如果调用线程没有循环器,则会引发
RuntimeException
。尝试以下方式更改代码:

private TimerTask syncTaskGps = new TimerTask() {
        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()); 
          Looper.loop();   
        }
    };

使用处理程序而不是计时器/计时器任务

public class MyService extends Service {
private Handler mTimer = new Handler();
private Runnable mTask = new Runnable() {
@Override
public void run() {
//DO SOMETHING HERE
mTimer.postDelayed(this, interval*1000L);
}
};

private int interval = 60; // 60 seconds

public void onCreate() {
mTimer.postDelayed(mTask, interval*1000L); //start the timer for the first time
}

public void onDestroy() {
if(mTimer != null) {
mTimer.removeCallbacks(mTask); //cancel the timer
}
}
}

通过使用处理程序,您可以随时重置间隔,例如,首先您希望间隔每5秒运行一次,而不是用户决定每1秒运行一次,只需更改间隔变量并再次调用mTimer.postDelayed。

使用处理程序而不是计时器/TimerTask

public class MyService extends Service {
private Handler mTimer = new Handler();
private Runnable mTask = new Runnable() {
@Override
public void run() {
//DO SOMETHING HERE
mTimer.postDelayed(this, interval*1000L);
}
};

private int interval = 60; // 60 seconds

public void onCreate() {
mTimer.postDelayed(mTask, interval*1000L); //start the timer for the first time
}

public void onDestroy() {
if(mTimer != null) {
mTimer.removeCallbacks(mTask); //cancel the timer
}
}
}


通过使用Handler,您可以随时重置间隔,例如,首先您希望间隔每5秒运行一次,而不是用户决定每1秒运行一次,只需更改间隔变量并再次调用mTimer.postdayed。

亲爱的blackbelt.its说方法getLooper()未为该行中的类型Looper定义。是否需要为两个TimerTask声明应用Looper。?这是getMainLooper(),感谢亲爱的blackbelt。需要添加两个TimerTask定义位置。?能否重新表述最后一句?我仍然不认为在与UI线程不同的线程中运行LocationManager有什么意义。为什么需要TimerTask?您有用于此目的的minTime参数。minTime位置更新之间的最小时间间隔,以毫秒为单位。阅读文档亲爱的blackbelt.it说该行中的类型Looper的方法getLooper()未定义。它是否需要为两个TimerTask声明应用Looper。?它是getMainLooper()。感谢亲爱的blackbelt。它需要添加两个TimerTask定义位置。?你能重新表述最后一句吗?我仍然不认为在与UI线程不同的线程中运行LocationManager有什么意义。为什么需要TimerTask?您有用于此目的的minTime参数。minTime位置更新之间的最小时间间隔,以毫秒为单位。阅读文档亲爱的用户3381480,那么如何将您的代码应用于我的问题。?我需要添加两个TimerSynchs。类型不匹配:无法在第16行将处理程序转换为布尔值编辑,应为if(mTimer!=null){然后只需设置mTimer2和mTask2以及不同的间隔(如果需要)。再次编辑,将其放入run()中就像你的代码一样。亲爱的用户3381480,那么你的代码如何应用于我的问题。?我需要添加两个TimerSynchs。类型不匹配:无法在第16行将处理程序转换为布尔值编辑,应为if(mTimer!=null){然后只需设置mTimer2和mTask2以及不同的间隔(如果需要)。再次编辑,将其放置在代码的run()中。