- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当我在 Activity 中使用 GoogleApiClient 但将其移至 Service 并且未调用 onConnected 时正在工作。
public class StepsMonitoringService extends Service implements GoogleApiClient.ConnectionCallbacks {
private GoogleApiClient mClient;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mClient = new GoogleApiClient.Builder(this).addApi(Fitness.HISTORY_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
.addConnectionCallbacks(this).build();
mClient.connect();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
mClient.disconnect();
}
@Override
public void onConnected(Bundle connectionHint) {
// NOT being called!! WHY??
}
@Override
public void onConnectionSuspended(int cause) {
}
有什么想法吗?我究竟做错了什么?有没有人让 GoogleApiClient 在服务中工作?
服务是从 Activity 中调用的
公共(public)类 MainActivity 扩展 FragmentActivity {
private TextView mStepsView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, StepsMonitoringService.class);
startService(intent);
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("StepMonitoringServiceIntent"));
mStepsView = (TextView) findViewById(R.id.steps);
}
private void displayOnUI(String msg) {
mStepsView.setText(msg + "\n" + mStepsView.getText());
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long steps = intent.getLongExtra("steps", 0);
displayOnUI(steps + " steps");
}
};
在调试过程中,我可以看到服务的 onCreate 被调用,但是 onConnected 从未被调用。据我所见,日志中没有什么特别之处。
最佳答案
首先实现这些接口(interface):
然后您必须将这些方法添加到您的类中:
一旦连接,OnConnected 方法就会被调用。在这个方法中你可以做任何你想做的事情,比如获取当前位置,位置地址,在 map 上添加一个标记点等等。
但是,如果不与 Google API 客户端建立连接,您将无法执行任何操作。要连接到 GoogleAPIClient,您可以将此方法添加到您的类中并在您的 onCreate 方法中调用它。
private synchronized void buildGoogleAPIClient(){
googleApiclient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
googleApiclient.connect();
Toast.makeText(getActivity(), "connected", Toast.LENGTH_SHORT).show();
}
关于android - GoogleApiClient onConnected 未被调用,在服务中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36299151/
当我尝试从 GoogleApiClient 注销时,收到以下错误消息 GoogleApiClient.isConnected() on a null object reference 这是我的代码:
所以我发现了一些关于 GoogleApiClient 对我来说不是很清楚的东西。GoogleApiClient 有一个名为 onConnected 的函数,该函数在客户端已连接(肯定)时运行。 我得到
我在这里查看了这个问答:GoogleApiClient is throwing "GoogleApiClient is not connected yet" AFTER onConnected fun
我正在尝试在服务中实现 Fused Location API。我已经完成了以下编码,但出现如下错误: java.lang.RuntimeException: Unable to instantia
我想在android中实现自动完成位置google API。为此,我需要创建一个 GoogleApiClient 对象。我为此使用了以下代码... private GoogleApiClient mG
我创建了一个名为 SendToWear 的类,其中包含一个名为 sendMessage(Context context, String path, @Nullable String data) 的静态
我想获取设备的当前位置,这是我的一段代码 private void displayLocation() { mLastLocation = LocationServices.FusedLoca
我的场景是我希望有一个服务定期获取一些信息(例如用户的位置),将其报告到我的服务器并在该位置与某个 X 位置匹配时接收推送通知。 无论应用程序是否正在运行,我都希望得到一些关于定期检索位置信息的最佳机
我正在将 Google Smart Lock 实现到一个应用程序中,之前我在构建 Api 客户端时没有遇到任何问题。事实上,我正在完成一些语法更改并清理代码(甚至没有触及初始化 Api 客户端的代码)
这是我尝试实例化它的代码: gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
我在我的应用程序上使用 Google+ 登录,并遵循了诸如 Getting Started 之类的引用和 Google+ Sign-in for Android . 所以情况是这样的: 我有一个 Lo
我正在编写一个 Android Wear 应用,我想让可穿戴设备在点击按钮时在手机上启动一个 Intent。 为此,我首先像这样设置与 GoogleApiClient 的连接 googleApiCl
我想使用 FusedLocationProviderApi 获取最后的已知位置。我按照谷歌教程所说的那样做了,但似乎 GoogleApiClient 没有连接,至少它没有触发 onConnected
我正在处理的应用程序有时可以正常工作,但有时会出现此错误 FATAL EXCEPTION: java.lang.RuntimeException: Unable to pause activity {
我正在为我的项目使用 Google App Indexing。 此功能的新手。我只是遵循谷歌指南。 Notify the google using API 在此页面中,要求创建一个 GoogleApi
我通过 GoogleApiClient 为应用设置了 google plus 登录。 每当应用程序首次安装并尝试通过 GoogleApiClient 建立连接时,它永远不会成功,并且总是以 onCon
我正在尝试获取服务中的位置更新信息。为此,我创建了一个服务,并在服务类的 onStartCommand 中创建了 GoogleApiClient,但我没有在服务中收到连接回调。请帮助解决这个问题。 下
更新用户的当前位置我在我的项目中实现了以下方法: GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedList
我正在尝试为我的 Android 游戏使用 Google Games 回合制 Api。我用来连接 GoogleApiClient 的代码来自 Google 的 Api 示例或文档。 在我的 onCon
我尝试学习 android 开发,现在我正在开发一个具有融合位置服务的应用程序。 当我想放置 GoogleMap fragment 、带有经纬度的 Textviews 和其他一些小部件时,我得到了一个
我是一名优秀的程序员,十分优秀!