gpt4 book ai didi

android - checkSelfPermission 空指针 6.0

转载 作者:行者123 更新时间:2023-11-29 00:06:15 29 4
gpt4 key购买 nike

我在检查权限时遇到问题,在 6.0 中总是说 nullpointer 我的调试:

我做错了什么

public String getGeolocation(Context context){

try{
int MY_PERMISSION_ACCESS_COURSE_LOCATION = 1000;

if ( ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {

ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSION_ACCESS_COURSE_LOCATION);
}
if ( Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//return 0 ;
}
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // Here is nullpointer
//Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
// Do something with the recent location fix
// otherwise wait for the update below
}
else {
//mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
}catch (Exception e){}
return geo;

}

//我用这个方法来获取数据:

public void onLocationChanged(Location location) {
if (location != null) {
Log.v("Location Changed", location.getLatitude() + " and " + location.getLongitude());
geo = ""+location.getLatitude() + " , " + location.getLongitude();
mLocationManager.removeUpdates(this);
}
}

Android studio 的错误异常:

java.lang.IllegalStateException: System services not available to Activities before onCreate()

我在哪里调用 getGeolocation() ?

public class AlarmReceiverCustom extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
getGeolocation(context)

最佳答案

您正在为 getGeolocation() 提供上下文,但您仍在对对象本身调用 getSystemService()

您可能想使用您传递的上下文:

mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

但是你必须考虑Receiver and Process Lifecycle也是:

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active. [...]

This means that for longer-running operations you will often use a Service in conjunction with a BroadcastReceiver to keep the containing process active for the entire time of your operation.

因此,您最好重新实现应用程序的这一部分并使用服务来更新位置。

关于android - checkSelfPermission 空指针 6.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33599097/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com