本文整理匯總了Java中com.google.android.gms.location.LocationClient.requestLocationUpdates方法的典型用法代碼示例。如果您正苦於以下問題:Java LocationClient.requestLocationUpdates方法的具體用法?Java LocationClient.requestLocationUpdates怎麽用?Java LocationClient.requestLocationUpdates使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.android.gms.location.LocationClient
的用法示例。
在下文中一共展示了LocationClient.requestLocationUpdates方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getCurrentLocation
import com.google.android.gms.location.LocationClient; //導入方法依賴的package包/類
@SuppressLint("HandlerLeak")
private void getCurrentLocation() {
if (isLocationServiceEnabled) {
handleLocationRequestTimeout();
int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
Log.d(SmartConstants.APP_NAME, "LocationService->play services:" + resp);
if (resp == ConnectionResult.SUCCESS) {
locationclient = new LocationClient(context, this, this);
locationclient.connect();
showProgressDialog();
final LocationListener locListener = this;
Handler locationHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
locationrequest = LocationRequest.create();
locationrequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationrequest.setInterval(TIME_INTERVAL_BW_REQUESTS);
locationrequest.setNumUpdates(1);
locationclient.requestLocationUpdates(locationrequest, locListener);
}
};
locationHandler.sendEmptyMessageDelayed(0, 1000);
} else {
onErrorLocationOperation(ExceptionTypes.LOCATION_ERROR_PLAY_SERVICE_NOT_AVAILABLE, ExceptionTypes.LOCATION_ERROR_PLAY_SERVICE_NOT_AVAILABLE_MESSAGE);
}
} else {
// Means that the location service of the device is not turned on
// Prompt the user to turn on the Location service
showDecisionDialog("Location Service Disabled", "Location service is disabled in your device. Enable it?", "Enable Location", "No");
}
}
示例2: LocationTracking
import com.google.android.gms.location.LocationClient; //導入方法依賴的package包/類
/**
* Subscribes for location updates
*
* The updates will be received with the interval INTERVAL_UPDATE (ms)
* @param context current context
* @param locationListener Callback
*/
public LocationTracking(final Context context, final LocationListener locationListener) {
locationclient = new LocationClient(context, new GooglePlayServicesClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Toast.makeText(context, "Connected to GPS!!", Toast.LENGTH_LONG)
.show();
locationclient.requestLocationUpdates(
LocationRequest.create().setInterval(INTERVAL_UPDATE)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY),
locationListener);
}
@Override
public void onDisconnected() {
Toast.makeText(context, "Disconnected from GPS!!", Toast.LENGTH_LONG)
.show();
}
}, new GooglePlayServicesClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(context, "Failed to connect to GPS!!", Toast.LENGTH_LONG)
.show();
}
}
);
locationclient.connect();
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(context,
"Please enable gps to get a more accurate location tracking", Toast.LENGTH_LONG)
.show();
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}