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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了接收广播Intent时出错{act = android.location.PROVIDERS_CHANGED flg = 0x10}相关的知识,希望对你有一定的参考价值。

我有这样的广播:

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时,位置Receiver应该执行requestLocationUpdates()。这是该方法中的代码:

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();
    }
答案

这是你的例外:

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

在使用之前,请确保您的mGoogleApiClient已连接。您可以使用mGoogleApiClient.isConnected()来验证和mGoogleApiClient.connect()连接它。

编辑:

您可以做的另一件事是确保在连接mGoogleApiClient后注册广播。您可以将它放在onConnected回调上。

另一答案

尝试将googleApiClient创建移动到onCreate()https://stackoverflow.com/a/29440779/4904995

以上是关于接收广播Intent时出错{act = android.location.PROVIDERS_CHANGED flg = 0x10}的主要内容,如果未能解决你的问题,请参考以下文章

权限拒绝:广播 Intent act=android.provider.Telephony.SMS_RECEIVED

广播接收器不会收到相机事件

广播接收器是不是依赖binder或intent,Android

从Intent服务发送短信 - 无法接收广播以检查是否发送了短信

8.0后广播在AndroidManifest.xml中注册后发送intent接收不到广播

android广播里怎么实现Intent页面跳转 广播接收到信息就跳转