0

我在我的 android 应用程序中使用 GoogleApiClient 并在我的应用程序恢复时遇到崩溃。

代码如下所示:

MainActivity 签名:

public class MainActivity : AppCompatActivity, GoogleApiClient.IOnConnectionFailedListener, Android.Gms.Location.ILocationListener

创建时

protected override async void OnCreate(Bundle bundle)
{
    App.Initialize();
    base.OnCreate(bundle);

    SetContentView(Resource.Layout.Main);

    await InitializeGoogleApis();
}

谷歌 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);
}

我打开了所有异常但没有异常描述

当我尝试恢复时,应用程序总是崩溃。当它崩溃时,它被置于后台,当我尝试再次恢复它时,它工作得很好。

4

1 回答 1

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 函数中重新初始化它。所以基本上这解决了我的问题。

于 2016-05-26T10:26:57.997 回答