我知道传递静态上下文是不可取的,应该避免。我理解它背后的原因。
我的问题是,我们如何访问某些在参数中需要"Activity“的调用。例如:
public class BackgroundServiceParent extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.HISTORY_API)
.addApi(Fitness.CONFIG_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
.useDefaultAccount()
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {
....
if (!result.hasResolution()) {
// Show the localized error dialog
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(),
(Activity) applicationContext, 0).show();
return;
}
if (!authInProgress) {
try {
authInProgress = true;
result.startResolutionForResult((Activity) applicationContext, REQUEST_OAUTH);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG,
Exception while starting resolution activity", e);
}
}
}
任何帮助理解这种情况下的最佳方法的人都将不胜感激。
发布于 2018-05-31 07:46:45
你不需要,从服务访问活动是不好的。您可以使用绑定器在您的活动中获取服务实例,然后您可以在您的服务中注册一些回调来与您的活动进行通信。请查看此链接以获取示例https://gist.github.com/ErikHellman/4b6ecc87d1b37c9809d1
https://stackoverflow.com/questions/50614718
复制相似问题