尝试使用延迟创建方法时出错

时间:2016-10-26 22:08:37

标签: java android google-maps

尝试延迟调用此方法:

  LocationServices.FusedLocationApi.requestLocationUpdates(GoogleApiClient, LocationRequest, (LocationListener) this);

我在堆栈上发现了导致延迟的方法:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    //Do something after 100ms
  }
}, 100);

但是当我试图像这样调用这个方法时:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    LocationServices.FusedLocationApi.requestLocationUpdates(GoogleApiClient, LocationRequest, (LocationListener) this);
  }
}, 100);

Android工作室更改此内容

       final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                    return;
                }
                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (LocationListener) this);
            }
        }, 100);

现在我的问题,android studio突出显示错误:

this

中的

(this, Manifest.permission.ACCESS_FINE_LOCATION) this

中的

(this, Manifest.permission.ACCESS_COARSE_LOCATION)(LocationListener) this

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (LocationListener) this);

如何使其发挥作用?

1 个答案:

答案 0 :(得分:0)

在Java中,this是一个关键字,它引用执行方法所在的上下文。在run()方法中this实际上是指由您创建的Runnable()类。处理程序。

您需要将活动上下文传递给Runnable()并在那些方法中使用它。