大佬教程收集整理的这篇文章主要介绍了Google Play服务或Android定位服务,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在我的应用程序中实现基于位置的功能.我一直在阅读,我发现自己有点困惑.
当谷歌搜索教程时,几乎每个结果都会返回一个使用Android LOCATIOn API的示例.
但是在阅读android开发人员指南时,他们会说明以下内容:
所以这告诉我不要去实现一个位置监听器的简单路线.
所以我的问题是,两者有什么区别?我为什么要使用一个而不是另一个?
我在哪里可以找到关于如何安全准确地正确访问Google Play服务位置API的体面教程.
到目前为止我已尝试过这个(如Android网站上所建议的那样)但是我的回调都没有被调用.
public class LOCATIOnManager implements GoogleApiClient.ConnectionCallBACks, GoogleApiClient.onConnectionFailedListener {
private Context mContext;
private GoogleApiClient mGoogleApiClient;
private LOCATIOn mLastLOCATIOn;
public LOCATIOnManager(Context context) {
mContext = context;
//
if (checkIfGooglePlayservicesAreAvailable()) {
//Get Access to the google service api
buildGoogleApiClient();
} else {
//Use Android LOCATIOn services
//TODO:
}
}
public LOCATIOn getCoarseLOCATIOn() {
if (mLastLOCATIOn != null) {
return mLastLOCATIOn;
} else return null;
}
private synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.builder(mContext)
.addConnectionCallBACks(this)
.addOnConnectionFailedListener(this)
.addApi(LOCATIOnservices.API)
.build();
}
private Boolean checkIfGooglePlayservicesAreAvailable() {
int errorCode = GooglePlayservicesUtil.isGooglePlayservicesAvailable(mContext);
if (errorCode != ConnectionResult.succesS) {
GooglePlayservicesUtil.getErrorDialog(errorCode, (MainActivity) mContext, 0).show();
return false;
}
return true;
}
@Override
public void onConnected(Bundle bundlE) {
LOCATIOn LOCATIOn = LOCATIOnservices.FusedLOCATIOnApi.getLastLOCATIOn(mGoogleApiClient);
if (LOCATIOn != null) {
mLastLOCATIOn = LOCATIOn;
Toast.makeText(mContext, LOCATIOn.getLongitude() + " , " + LOCATIOn.getLatitude() + " : " + LOCATIOn.getAccuracy(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionSuspended(int i) {
Toast.makeText(mContext, "suspended", Toast.LENGTH_LONG).show();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(mContext, connectionResult.toString(), Toast.LENGTH_LONG).show();
}
}
LOCATIOnManager LOCATIOnManager = new LOCATIOnManager(this);
LOCATIOn LOCATIOn = LOCATIOnManager.getCoarseLOCATIOn();
//Use LOCATIOn
我想创建一个帮助类,我可以从任何活动或片段调用它.
但是,当我运行以下命令时,构造函数执行成功.但是我的回调中没有一个断点被击中.即使在1或2分钟后.
解决方法:
Google Play服务Api使用一个回调方法public void onConnected(Bundle bundlE),只有在建立连接时才会调用它.只有这样,LOCATIOnservices.FusedLOCATIOnApi.getLastLOCATIOn(googleApiClient)方法调用才能返回正确的LOCATIOn.
您已构建了一个帮助程序类来获取此位置,但您应该了解该连接未立即建立.因此,只是对辅助类或其方法的异步调用可能会返回null对象,因为可能尚未建立连接.
您可以通过以下方式获取位置.
1)广播位置并通过BroadcastReceiver接收它以从公共void onConnected(Bundle bundlE)内执行后续操作.这对我有用.
private Boolean flag = false;
@Override
public void onConnected(Bundle connectionHint) {
LOCATIOn = LOCATIOnservices.FusedLOCATIOnApi.getLastLOCATIOn(googleApiClient);
if (LOCATIOn != null && !flag) {
flag = true;
Intent intent = new Intent(LOCATION_BroaDCAST_ACTION);
intent.putExtra(INTENT_EXTRA_LOCATION, LOCATIOn);
LocalBroadcastManager.geTinstance(context).sendBroadcast(intent);
Log.i(tag, "Sending LOCATIOn Broadcast");
Log.i(tag, LOCATIOn.toString());
} else if (LOCATIOn == null){
Toast.makeText(mContext, R.String.no_LOCATIOn_detected, Toast.LENGTH_LONG).show();
Log.e(tag, "No LOCATIOn Detected");
}
}
2)在调用Activity或Fragment中创建一个公共LOCATIOn对象,并从public void onConnected(Bundle bundlE)方法中更新其值.
@Override
public void onConnected(Bundle connectionHint) {
LOCATIOn = LOCATIOnservices.FusedLOCATIOnApi.getLastLOCATIOn(googleApiClient);
if (LOCATIOn != null) {
MyActivity.LOCATIOn = LOCATIOn; // update the LOCATIOn object of you caller activity or fragment
Log.i(tag, "LOCATIOn updated");
}
}
此外,您应该在构建GoogleApiClient后连接到Google Play服务位置Api.只需在buildGoogleApiClient()之后添加方法@L_41_9@mGoogleApiClient.connect();在构造函数中调用方法.您无需检查Google Play服务是否可用.
您还可以添加方法调用googleApiClient.connect();在你的public void onConnectionSuspended(int i)的实现中.
以上是大佬教程为你收集整理的Google Play服务或Android定位服务全部内容,希望文章能够帮你解决Google Play服务或Android定位服务所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。