【发布时间】:2021-01-02 06:23:11
【问题描述】:
我正在尝试在我的应用程序中访问 GPS。 我已授予权限,但仍然出现错误。
如果我将导入而不是 import com.google.android.gms.location.LocationListener 到 import android.location.LocationListener;
和
mLocService.requestLocationUpdates(mLocProvider.getName(), getGpsUpdatePeriod(prefs), getGpsDistanceUpdatePeriod(prefs), (android.location.LocationListener) this);
代码正在编译,但它没有进入 if() 条件并且“gps 权限--->2”没有进入日志。
import com.google.android.gms.location.LocationListener;
public class MainActivity extends RoboActivity implements ObdProgressListener, LocationListener, GpsStatus.Listener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gpsStart();
}
private synchronized void gpsStart() {
if (!mGpsIsStarted && mLocProvider != null && mLocService != null && mLocService.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.d(TAG, "gps permission--->1");
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Log.d(TAG, "gps permission--->2");
mLocService.requestLocationUpdates(mLocProvider.getName(), getGpsUpdatePeriod(prefs), getGpsDistanceUpdatePeriod(prefs), (com.google.android.gms.location.LocationListener) this);
mGpsIsStarted = true;
return;
}
} else {
Log.d(TAG, "gps permission--->3");
gpsStatusTextView.setText(getString(R.string.status_gps_no_support));
}
}
在android中获取将第4个参数转换为'android.location.LocationListener'。
如果我像下面这样给出,
mLocService.requestLocationUpdates(mLocProvider.getName(), getGpsUpdatePeriod(prefs), getGpsDistanceUpdatePeriod(prefs), (android.location.LocationListener) this);
出现错误**无法转换为 android.location.LocationListener**
【问题讨论】:
-
显示更多代码并提供更详细的问题描述。事实上目前没有问题......你的班级有
implements LocationListener吗? -
感谢您的回复。当然,我会的。
标签: android gps android-permissions android-gps locationlistener