我在我的Android应用中使用GoogleApiClient,并在我的应用恢复时遇到崩溃。
代码如下所示:
MainActivity签名:
public class MainActivity : AppCompatActivity, GoogleApiClient.IOnConnectionFailedListener, Android.Gms.Location.ILocationListener
OnCreate中
protected override async void OnCreate(Bundle bundle)
{
App.Initialize();
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
await InitializeGoogleApis();
}
Google API初始化
private async Task InitializeGoogleApis()
{
// Construct api client and request for required api's
googleApiClientBuilder = new GoogleApiClient.Builder(this)
.AddApi(LocationServices.API)
.EnableAutoManage(this, this);
// Connect to google play services
googleApiClient = await googleApiClientBuilder
.BuildAndConnectAsync();
// Request location api and set common properties
googleLocationRequest = new LocationRequest()
.SetPriority(LocationRequest.PriorityHighAccuracy)
.SetInterval(1000)
.SetFastestInterval(1000);
// Set location changed listener
await LocationServices.FusedLocationApi.RequestLocationUpdatesAsync(googleApiClient, googleLocationRequest, this);
}
OnResume和OnDestroy :
protected override void OnResume()
{
base.OnResume();
}
protected override async void OnDestroy()
{
base.OnDestroy();
if (googleApiClient != null)
await LocationServices.FusedLocationApi.RemoveLocationUpdatesAsync(googleApiClient, this);
}
我已启用所有例外但is no exception description
当我尝试恢复时,应用程序总是崩溃。当它崩溃时,它会被放入后台,当我再次尝试恢复时,它会完美运行。
答案 0 :(得分:0)
好的,我找到了一个讨厌的方法,我把所有的实现都保持不变,只是将这些功能添加到我的主要活动中
protected override void OnStop()
{
base.OnStop();
if (googleApiClient != null)
{
LocationServices.FusedLocationApi.Dispose();
googleApiClient.StopAutoManage(this);
googleApiClient.Disconnect();
googleApiClient.Dispose();
googleApiClient = null;
}
}
protected override async void OnRestart()
{
base.OnRestart();
await InitializeGoogleApis();
}
我强行停止GoogleApiClient并在我的OnRestart函数中重新初始化它。所以基本上解决了我的问题。