android - checkSelfPermission 空指针 6.0

标签 android

我在检查权限时遇到问题,在 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/

相关文章:

java - 为什么我的应用程序在启动时不断崩溃 - Android

android - 如何将应用程序中的图片分享到 Whatsapp?

Android 评分栏 : Cannot fill in a partial ratingBar if I customise it's color

android - psyonspotify 应用程序未加载最新专辑

java - android构建错误: can't find symbol class IntDef

android - 如何隐藏来电的默认屏幕

android - Gradle 找不到 android_native_app_glue

android - 在 Stripe Android CardWidget 如何更改提示文本大小和提示文本?

java - 如何将 rx.Observable<java.util.list<T>> 更改为 java.util.list<T>

android - 在 Android Wear 上运行 Android 文本转语音?