android - 接收广播 Intent 时出错 { act=android.location.PROVIDERS_CHANGED flg=0x10 }

标签 android gps broadcastreceiver

我的广播是这样的:

private final BroadcastReceiver locationReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().matches(LocationManager.PROVIDERS_CHANGED_ACTION)){
            //do stuff
            startLocationUpdates();
        }
    }
};

当用户打开 GPS 时,locationReceiver 应该执行 statLocationUpdates()。这是该方法内的代码:

protected void startLocationUpdates() {
    try
    {
        LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
        {
            buildAlertMessageNoGps();
        }
    }
    catch (SecurityException ex)
    {

    }

LocationServices.FusedLocationApi 线路有问题...我不明白为什么。但这就是日志所说的:

02-28 21:31:40.747 21273-21273/com.theapplabperu.hangover E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.theapplabperu.hangover, PID: 21273
                                                                        java.lang.RuntimeException: Error receiving broadcast Intent { act=android.location.PROVIDERS_CHANGED flg=0x10 } in com.theapplabperu.hangover.View.Activity.ActivitySplash$11@f6e8393
                                                                            at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:932)
                                                                            at android.os.Handler.handleCallback(Handler.java:815)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                            at android.os.Looper.loop(Looper.java:207)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5728)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
                                                                         Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet.

我读了另一篇文章,但我还不知道如何解决它。有什么想法吗?

更新:

我实际上在 onCreate 方法中创建了我的 googleapiclient:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    myContext = this;
    myActivity = this;
    mVisible = true;


    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(myContext)
                .addConnectionCallbacks(ActivitySplash.this)
                .addOnConnectionFailedListener(ActivitySplash.this)
                .addApi(LocationServices.API)
                .build();
    }

最佳答案

这是你的异常(exception):

Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet.

使用前请确保您的mGoogleApiClient已连接。您可以使用 mGoogleApiClient.isConnected() 进行验证,并使用 mGoogleApiClient.connect() 进行连接。

编辑:

您可以做的另一件事是确保在连接 mGoogleApiClient 后注册广播。您可以通过将其放在 onConnected 回调中来实现此目的。

关于android - 接收广播 Intent 时出错 { act=android.location.PROVIDERS_CHANGED flg=0x10 },我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49041795/

相关文章:

android - Unity GearVR 去掉 "requesting permission..."

java - 在 Android 设备中切换 Wifi 状态

html - 通过 html 调用 GPS 应用程序

android - 收到短信时联系人姓名未显示在通知中

android - 除非手动注册,否则 BroadcastReceiver 无法正常工作

android - 奇怪的 "Receiver not registered"异常

用于 HEVC 的 Android MediaCodec

java - Android:需要帮助理解变量赋值背后的 &0x04

Android - notifyDataSetChanged() 不适用于自定义适配器?

javascript - navigator.geolocation.watchPosition 以速度返回 NaN