在片段中使用 enableAutoManage()

Posted

技术标签:

【中文标题】在片段中使用 enableAutoManage()【英文标题】:using enableAutoManage() in fragment 【发布时间】:2015-08-17 19:53:48 【问题描述】:

还有其他方法可以连接 Google API 客户端吗?

我使用自动完成的地方,我必须在 MYFRAGMENT 中的某个地方使用此代码

mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
                .addApi(Places.GEO_DATA_API)
                .enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
                .addConnectionCallbacks(this).build();

我的问题

enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
                    .addConnectionCallbacks(this).build();

我无法处理它,因为当我将 this 替换为 getActivity() 时,我在转换时遇到了很多问题

感谢您的帮助,如果这个问题很愚蠢,我们深表歉意。

【问题讨论】:

您要替换哪个“this”?只有第一个?你的活动是 FragmentActivity 吗? enableAutoManage() 中的第一个“this”我有一个错误,我通过 cating 到 FragmentActivity 来修复它,但是当运行应用程序时它停止了 不,我的 MainActivity 不是 FragmentActivity @Hamza 你能告诉我 GOOGLE_API_CLIENT_ID 的类型。作为 enableAutoManage 接受 int 但我们的 google api 密钥是字符串。 【参考方案1】:

如果你想使用enableAutoManage,那么你必须让你的活动扩展FragmentActivity。它进行的回调是GoogleApiClient 的自动管理工作所必需的。因此,最简单的解决方案是将extends FragmentActivity 添加到您的活动中。这样你的演员就不会失败并导致应用在运行时崩溃。

另一种解决方案是自己管理 api 客户端。您将从构建器中删除 enableAutoManage 行,并确保您自己从客户端中删除 connect/disconnect。最常见的地方是onStart()/onStop()。比如……

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
            .addApi(Places.GEO_DATA_API)
            .addConnectionCallbacks(this).build();


@Override
protected void onStart() 
    super.onStart();
    mGoogleApiClient.connect();


@Override
protected void onStop() 
    super.onStop();
    mGoogleApiClient.disconnect();

【讨论】:

真的只是连接/断开连接吗?通过查看 GoogleApiClient 来源,它似乎做了更多的事情。我不能确定,因为很难阅读他们的混淆代码。我只是不想从FragmentActivity 扩展,因为我的应用程序不支持旧的 API 级别。从FragmentActivity 扩展也会带来更多问题(动画、不同的FragmentManager 和LoaderManager.LoaderCallbacks),所以我最好还是留在Activity。但我想模仿enableAutoManage() 的确切行为。 GoogleApiClient 的文档建议除了启动和停止行为之外,它还尝试处理一些连接错误。如果您不启用AutoManager,那么您必须自己在添加到客户端的GoogleApiClient.ConnectionCallbacks 中添加此逻辑。很难说他们在这些情况下采取了哪些具体行动。 问题是:谷歌提供的示例代码使用AppCompatActivity而不是片段,但仍然使用enableAutoManage。这怎么可能? 我不确定您指的是哪个示例代码。您的意思是说他们使用AppCompatActivity 而不是FragmentActivity?这是可能的,因为AppCompatActivity 扩展了FragmentActivity 我肯定会推荐查看新的 GeoDataClient 类。它可以让您跳过处理 GoogleApiClient 类的麻烦。 (developers.google.com/android/reference/com/google/android/gms/…)【参考方案2】:

抱歉,回复晚了,但您可以扩展 AppCompatActivity,而不是扩展 FragmentActivity...

public class YourActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener 

.....

mCredentialsApiClient = new GoogleApiClient.Builder(context)
                    .addConnectionCallbacks(this)
                    .enableAutoManage(this,this)
                    .addApi(Auth.CREDENTIALS_API)
                    .build();

【讨论】:

【参考方案3】:

如果您的片段在 FragmentActivity 或 AppCompatActivity 中运行,您可以执行以下操作:

        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .enableAutoManage((FragmentActivity) getActivity() /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() 
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) 
                    // your code here
                
            )
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

【讨论】:

【参考方案4】:

我的解决方案类似于接受的答案,除了我使用 Builder 的第二个签名,以便将 connectionFailedListener 也发送到构造函数。

onStart()onStop() 中分别为mGoogleApiClient.connect()mGoogleApiClient.disconnect()

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    mGoogleApiClient = new GoogleApiClient.Builder(this /*context*/ , this /*connectedListener*/, this /**connectionFailedListener/)
            .addApi(Places.GEO_DATA_API)
            .build();

【讨论】:

以上是关于在片段中使用 enableAutoManage()的主要内容,如果未能解决你的问题,请参考以下文章

addApi(Games.API) 已弃用

如何在另一个片段中访问和使用一个片段的按钮?

片段活动捕获 onKeyDown 并在片段中使用

当我们在android中使用backstack返回上一个片段时,上一个片段正在重新启动

如何在片段中而不是在活动中使用 NavController(在片段中带有 NavHost)?

在活动中使用 OnBackPressed 方法的一个片段到另一个片段