繁体   English   中英

Maintask中run方法中的Android异常

[英]Android Exception in run Method in a Maintask

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

我有一个服务,每15秒调用run MainTaskrun方法(出于测试原因) timer.scheduleAtFixedRate(new MainTask(), 0, 15000); (在onStartCommandonStartCommand

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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