简体   繁体   中英

Android Exception in run Method in a Maintask

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

I got a service calling the run method of my MainTask every 15 seconds (for testing reasons) timer.scheduleAtFixedRate(new MainTask(), 0, 15000); (called in onStartCommand .

The code in the run method is looking like this:

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!
        }

The GpsHelper class is looking like this (it has some if statements - because it is also used outside the Service in the Mainactivity :

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();
                }
            }
        }

    }

After getting the position this method is called: mLocationManager.removeUpdates(this);

When is this error occurring? The fourth time run is getting called

What does an possible answer need to contain? An answer should help me to fix this error without changing any logical behaviour and should not run on the ui thread !

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
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM