维护任务中运行方法的Android异常



java.lang.RuntimeException:无法在未调用Looper.prepare((的线程内创建处理程序

我得到了一个每隔15秒调用MainTaskrun方法的服务(出于测试原因(timer.scheduleAtFixedRate(new MainTask(), 0, 15000);(在onStartCommand.中调用

run方法中的代码如下所示:

public void run() {
        Log.v("MainTask", "run() Called");
        GpsHelper gpsHelper = new GpsHelper(getApplicationContext(), this); // This is causing the error - commented out => no error
        // Doing some independent webrequests here!
        }

GpsHelper类看起来是这样的(它有一些if语句,因为它也在Mainactivity:中的Service之外使用

public GpsHelper(Context context, LocationReceiver locationReceiver) {
    this.context = context;
    this.locationReceiver = locationReceiver;
    mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    if (locationReceiver instanceof Runnable) {
        isRunnable = true;
    }
    run();
}
/**
 * Getting the Location, handling permission, requesting location updates if time is greater than 2 minutes
 */
public void getLocation() {
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && !isRunnable) {
        StaticHelpers.askForFineLocationPermission(context);
    } else {
        if (isRunnable && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Log.v("GpsHelper", "Runnable can't get position - check permissions!");
        } else {
            Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            // If time not greater than 2 minutes
            // TODO: Move to resources
            if (location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
                Log.v("GpsHelper", "Using existing location");
                locationReceiver.onLocationAvailable(location);
            } else {
                Log.v("GpsHelper", "No Position Available => Requesting Location Updates");
                mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
                if (!isRunnable && !mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    showSettingsDialog();
                }
            }
        }
    }

获得位置后,调用此方法:mLocationManager.removeUpdates(this);

此错误何时发生第四次运行被称为

一个可能的答案需要包含什么答案应该可以帮助我在不改变任何逻辑行为的情况下修复此错误,并且不应该在ui线程上运行

getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Log.v("MainTask", "run() Called");
            GpsHelper gpsHelper = new GpsHelper(getApplicationContext(), this); // This is causing the error - commented out => no error
        }
    });

最新更新