[英]Error receiving broadcast Intent { act=android.location.PROVIDERS_CHANGED flg=0x10 }
I have broadcast like this: 我已经这样广播了:
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();
}
}
};
When the user turns on the gps, locationReceiver should executre the statLocationUpdates(). 当用户打开gps时,locationReceiver应该执行statLocationUpdates()。 This is the code inside that method:
这是该方法中的代码:
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)
{
}
There is a problem with line LocationServices.FusedLocationApi... I cant figure out why. LineService.FusedLocationApi行有问题...我不知道为什么。 But this is what the log says:
但这就是日志所说的:
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.
I read another post but I didn't get how to solve it yet. 我读了另一篇文章,但还没有解决方法。 Any idea?
任何想法?
UPDATE: 更新:
I actually create my googleapiclient in the onCreate method: 我实际上是在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();
}
It's on your exception: 这是您的例外:
Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
Please make sure that your mGoogleApiClient
is connected before using it. 使用前请确保您的
mGoogleApiClient
已连接。 You may use mGoogleApiClient.isConnected()
to verify and mGoogleApiClient.connect()
to connect it. 您可以使用
mGoogleApiClient.isConnected()
进行验证,并使用mGoogleApiClient.connect()
进行连接。
Edit: 编辑:
Another thing you can do is to make sure you register the broadcast after the mGoogleApiClient has been connected. 您可以做的另一件事是确保在连接mGoogleApiClient之后注册广播。 You may do so by placing it on the
onConnected
callback. 您可以将其放在
onConnected
回调中。
Try moving your googleApiClient creation to onCreate()
. 尝试将googleApiClient创建的内容移动到
onCreate()
。 https://stackoverflow.com/a/29440779/4904995 https://stackoverflow.com/a/29440779/4904995
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.