【问题标题】:Getting error on requestLocationUpdates in JobInetentService in Oreo在 Oreo 的 JobInetentService 中的 requestLocationUpdates 出现错误
【发布时间】:2018-09-28 17:15:55
【问题描述】:

我编写了一个基于 GPS 的定位服务,我在其中调用 requestLocationUpdates 以获取更新的位置并使其正常工作,但是当我定位到 Android Oreo 时我已将Service 替换为JobIntentService。但是当我运行应用程序时,出现以下错误。请提供解决方案。

java.lang.RuntimeException: An error occurred while executing doInBackground()                                                                              at android.os.AsyncTask$3.done(AsyncTask.java:361)
                                                                           at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
                                                                           at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
                                                                           at java.util.concurrent.FutureTask.run(FutureTask.java:271)
                                                                           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
                                                                           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                                                                           at java.lang.Thread.run(Thread.java:764)
                                                                        Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
                                                                           at android.os.Handler.<init>(Handler.java:204)
                                                                           at android.os.Handler.<init>(Handler.java:118)
                                                                           at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:234)
                                                                           at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:234)
                                                                           at android.location.LocationManager.wrapListener(LocationManager.java:872)
                                                                           at android.location.LocationManager.requestLocationUpdates(LocationManager.java:885)
                                                                           at android.location.LocationManager.requestLocationUpdates(LocationManager.java:470)
                                                                           at com.example.services.LocationService.onHandleWork(LocationService.java:49)
                                                                           at android.support.v4.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:391)
                                                                           at android.support.v4.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:382)
                                                                           at android.os.AsyncTask$2.call(AsyncTask.java:333)
                                                                           at java.util.concurrent.FutureTask.run(FutureTask.java:266)
                                                                           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
                                                                           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
                                                                           at java.lang.Thread.run(Thread.java:764) 

我的班级

public class LocationService extends JobIntentService {
LocationManager MylocationManager;
LocationListenerClass MylocationListener;
Context context;
Location mLocation;

@Override
public void onCreate() {
    super.onCreate();
    context = this;
}

public static void enqueueWork(Context context, Intent work) {
   enqueueWork(context, LocationService.class, Constants.JOB_ID, work);

}

@Override
protected void onHandleWork(@NonNull Intent intent) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

    } else {
        MylocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        MylocationListener = new LocationListenerClass();
        MylocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 36000000, 5000, MylocationListener);

    }
}

@Override
public void onDestroy() {
    super.onDestroy();
}

public class LocationListenerClass implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {

        if (location == null) {
            return;
        }
       mLocation = location;
        try {
            if (MylocationManager != null) {
                MylocationManager.removeUpdates(MylocationListener);

                MylocationManager = null;
            }
          } catch (Exception e) {

        }
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

        if (MylocationManager != null) {
            MylocationManager.removeUpdates(MylocationListener);

            MylocationManager = null;
        }


    }
}

}

我正在通过以下代码开始服务

Intent intent1 = new Intent(context, LocationService.class);
LocationService.enqueueWork(context, intent1);

【问题讨论】:

  • 我做了;没有发现错误,但是如果您在后台线程中执行一些 UI 任务,就会发生这种崩溃。
  • 但我没有执行任何 UI 任务
  • This 为我工作

标签: android location android-8.0-oreo android-gps jobintentservice


【解决方案1】:

问题是 LocationManager 的实现是创建一个处理程序。只能在调用 Looper.loop() 的线程上创建处理程序。例如,UI 线程(框架调用它的地方)或 HandlerThread(使用 Loopers)。 IntentService 在自己的线程上运行,并且没有 Looper。您需要在另一个线程上调用 requestLocation 更新,无论是您创建的 HandlerThread 还是 UI 线程都是最简单的。

【讨论】:

  • 你会提供这个例子吗?如何创建Handler...?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多