简体   繁体   English

连接到Google Fit Android API不在活动类中

[英]Connecting to google fit android API not in the activity class

I am currently learning to code for Android, and I have some troubles to understand how to correctly connect to APIs, or in my particular case to Google fit API. 我目前正在学习为Android编写代码,并且在理解如何正确连接到API或在我的特定情况下连接到Google Fit API方面遇到一些麻烦。

In the example provided by Google developers, they connect to API in onCreate() method. 在Google开发人员提供的示例中,他们使用onCreate()方法连接到API。 However, I was thinking if it was the best practice when developing a more complex application, and when it is ok to leave the code connecting to the API in the activity class? 但是,我在考虑这是开发更复杂的应用程序时的最佳实践,什么时候可以将代码连接到活动类中的API? To be more specific, when this should be moved to other threads? 更具体地说,何时应将其移至其他线程? I am concerned about performance issues keeping the code this way - on the other hands, is it possible that if I move this code to other threads, I may face some connection issues related to android service killing when running in the background? 我担心以这种方式保留代码的性能问题-另一方面,如果我将此代码移到其他线程,是否有可能在后台运行时遇到一些与Android服务终止有关的连接问题?

https://github.com/googlesamples/android-fit/blob/master/BasicHistoryApi/app/src/main/java/com/google/android/gms/fit/samples/basichistoryapi/MainActivity.java https://github.com/googlesamples/android-fit/blob/master/BasicHistoryApi/app/src/main/java/com/google/android/gms/fit/samples/basichistoryapi/MainActivity.java

protected void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FitnessOptions fitnessOptions = FitnessOptions.builder()
            .addDataType(TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .build();


    if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(this), fitnessOptions)) {
        GoogleSignIn.requestPermissions(
                this,
                REQUEST_OAUTH_REQUEST_CODE,
                GoogleSignIn.getLastSignedInAccount(this),
                fitnessOptions);
    } else {
        accessGoogleFit();
    }
}

I apologies in advance if my question looks stupid and basic, but I am very keen to learn more about best practices when it comes to coding and understand how to develop working applications! 如果我的问题看起来很愚蠢和基本,我会事先道歉,但是我非常想学习编码方面的最佳实践,并了解如何开发有效的应用程序!

To give you a better understanding, USUALLY any example given by google (or most people really) is in its most simple form just to give you an understanding of the code or the framework, how you decide to implement it best is up to you, which you will gain from experience. 为了让您更好地理解,通常Google(或大多数人)给出的任何示例都以其最简单的形式出现,只是为了让您对代码或框架有所了解,如何决定最好地实现它取决于您自己,您将从经验中受益。

To further answer your question, it really depends on the situation. 要进一步回答您的问题,这实际上取决于情况。 Leaving the api call in the create of the activity is probably fine if your activity isn't going to be created or recreated a number of times (even if it IS destroyed and recreated, it still might be fine depending on the situation), but yes, ideally this call should be moved out to different threads, using something like RxJava or other threading techniques. 如果不打算多次创建或重新创建活动,则将api调用留在活动的创建中可能很好(即使销毁并重新创建了它,根据情况仍然可以),但是是的,理想情况下,应使用RxJava或其他线程技术将此调用移到不同的线程中。 The reason for this is because (when api calls are done on a main thread) it could potentially suspend your entire app until that api call returns with information, which is bad practice and will give you a lot of problems. 这样做的原因是因为(当在主线程上完成api调用时)它可能会暂停整个应用程序,直到该api调用返回信息为止,这是错误的做法,并且会给您带来很多问题。 Moving this code out to different theads will not worsen performance (quite the opposite) and you should probably try to implement this code is some form of thread. 将此代码移至不同的theads不会降低性能(反之亦然),您可能应该尝试以某种形式的线程来实现此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM