I need to use mGoogleApiClient
across several activities so I decided to create a class MyApplication
which extends
the Application
class:
我需要跨多个活动使用mGoogleApiClient,所以我决定创建一个扩展Application类的MyApplication类:
public class MyApplication extends Application {
public static GoogleApiClient mGoogleApiClient;
public Context mContext;
@Override
public void onCreate() {
super.onCreate();
mGoogleApiClient = new GoogleApiClient.Builder(mContext)
.addConnectionCallbacks(mContext)
.addOnConnectionFailedListener(mContext)
.addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)
.addApi(Games.API).addScope(Games.SCOPE_GAMES)
.build();
}
@Override
public void onTerminate() {
super.onTerminate();
}
}
However there is a problem with these two lines:
但是这两行存在问题:
.addConnectionCallbacks(mContext)
.addOnConnectionFailedListener(mContext)
since I need to implement
因为我需要实施
GoogleApiClient.ConnectionCallbacks
and GoogleApiClient.OnConnectionFailedListener
GoogleApiClient.ConnectionCallbacks和GoogleApiClient.OnConnectionFailedListener
and then override these methods:
然后覆盖这些方法:
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
Is the Application
class allowed to implement
an interface
in general and should I do so, or should I delete those two lines from the mGoogleApiClient
and possibly initialize them in other activities?
是否允许Application类实现一般的接口,我应该这样做,还是应该从mGoogleApiClient中删除这两行,并可能在其他活动中初始化它们?
I don't know how the Application
class will handle these methods and the connection state and whether I need to implement these methods again in my other activities.
我不知道Application类将如何处理这些方法和连接状态,以及我是否需要在其他活动中再次实现这些方法。
Any advice?
1 个解决方案
#1
Your MyApplication
is a subclass of Application
specific only to your app. Android is only going to use the Application
portion of it, but you're allowed to implement whatever you need.
您的MyApplication是Application的子类,仅适用于您的应用。 Android只会使用它的Application部分,但是你可以实现你需要的任何东西。
But you should be aware of the fact that there can only be one instance of the application class at a time.
但是您应该意识到一次只能有一个应用程序类的实例。
If you need multiple callbacks, in a case where they store state for example, having MyApplication
implement them would not work that well due to the state changes. Instantiating an inner class would be a better option in that case.
如果您需要多个回调,例如,在它们存储状态的情况下,由于状态更改,让MyApplication实现它们将无法正常工作。在这种情况下,实例化内部类将是更好的选择。
Here it looks like you're only going to use the callbacks once, so having MyApplication
implement them would work just fine.
在这里看起来你只会使用一次回调,所以让MyApplication实现它们就可以了。
The application class is a good place for instantiating things like API clients. This ensures that no more than one is created and that one is always available.
应用程序类是实例化API客户端之类的好地方。这可确保创建的不超过一个并且始终可用。
Note also that Application
is a context, so you can use this
instead of mContext
.
另请注意,Application是一个上下文,因此您可以使用它而不是mContext。
#1
Your MyApplication
is a subclass of Application
specific only to your app. Android is only going to use the Application
portion of it, but you're allowed to implement whatever you need.
您的MyApplication是Application的子类,仅适用于您的应用。 Android只会使用它的Application部分,但是你可以实现你需要的任何东西。
But you should be aware of the fact that there can only be one instance of the application class at a time.
但是您应该意识到一次只能有一个应用程序类的实例。
If you need multiple callbacks, in a case where they store state for example, having MyApplication
implement them would not work that well due to the state changes. Instantiating an inner class would be a better option in that case.
如果您需要多个回调,例如,在它们存储状态的情况下,由于状态更改,让MyApplication实现它们将无法正常工作。在这种情况下,实例化内部类将是更好的选择。
Here it looks like you're only going to use the callbacks once, so having MyApplication
implement them would work just fine.
在这里看起来你只会使用一次回调,所以让MyApplication实现它们就可以了。
The application class is a good place for instantiating things like API clients. This ensures that no more than one is created and that one is always available.
应用程序类是实例化API客户端之类的好地方。这可确保创建的不超过一个并且始终可用。
Note also that Application
is a context, so you can use this
instead of mContext
.
另请注意,Application是一个上下文,因此您可以使用它而不是mContext。