本文介绍了如何从IntentService获取FusedLocation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 如何使用新的
来自 IntentService 的API - 如何确保 IntentService 会从 FusedLocation API 接收所有
回调?
即
如何获取位置对象同步?
解决方案
import java.util.ArrayList ;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
导入android.app.AlertDialog;
导入android.app.Notification;
导入android.app.NotificationManager;
导入android.app.PendingIntent;
导入android.app.Service;
导入android.content.Context;
导入android.content.Intent;
导入android.content.SharedPreferences;
导入android.location.Criteria;
导入android.location.Location;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.os.Handler;
导入android.os.IBinder;
导入android.util.Log;
导入android.widget.Toast;
import com.example.driverapplication.CommonUtilities;
import com.example.driverapplication.R;
import com.example.driverapplication.ServiceHandler;
import com.example.driverapplication.utilities.ConnectionDetector;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
public class LocationUpdate extends Service实现GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG =DRIVER;
私人LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 30000;
private static final float LOCATION_DISTANCE = 0;
private double currentLat,currentLng;
private SharedPreferences pref;
private String driverId;
私有GoogleApiClient mGoogleApiClient;
//连接位置服务的请求
private LocationRequest mLocationRequest;
private LocationListener locationListener;
private class LocationListener implements
com.google.android.gms.location.LocationListener {
public LocationListener(){
}
@Override
public void onLocationChanged(Location location){
Log.e(TAG,onLocationChanged:+ location);
currentLat = location.getLatitude();
currentLng = location.getLongitude();
$ b @Override
public IBinder onBind(Intent arg0){
返回null;
}
@Override
public int onStartCommand(Intent intent,int flags,int startId){
Log.e(TAG,onStartCommand);
super.onStartCommand(intent,flags,startId);
boolean stopService = false;
if(intent!= null)
stopService = intent.getBooleanExtra(stopservice,false);
System.out.println(stopservice+ stopService);
locationListener = new LocationListener();
if(stopService)
stopLocationUpdates();
else {
if(!mGoogleApiClient.isConnected())
mGoogleApiClient.connect();
}
return START_STICKY;
}
@Override
public void onCreate(){
Log.e(TAG,onCreate);
pref = getSharedPreferences(driver_app,MODE_PRIVATE);
driverId = pref.getString(driver_id,);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
$ b @覆盖
public void onDestroy(){
Log.e(TAG,onDestroy);
super.onDestroy();
}
public void stopLocationUpdates(){
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient,locationListener);
if(mGoogleApiClient.isConnected())
mGoogleApiClient.disconnect();
$ b @覆盖
public void onConnectionFailed(ConnectionResult arg0){
// TODO自动生成的方法存根
}
@Override
public void onConnected(Bundle arg0){
// TODO自动生成的方法存根
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setInterval(35000);
mLocationRequest.setFastestInterval(30000);
startLocationUpates();
}
private void startLocationUpates(){
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient,mLocationRequest,locationListener);
@Override
public void onConnectionSuspended(int arg0){
// TODO自动生成的方法存根
}
}
- How to retrieve current location by using the new FusedLocationAPI from an IntentService?
- How can i make sure that IntentService will receive allcallbacks from FusedLocation API?ie,How can i obtain the location object synchronously?
解决方案
This may help you
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import com.example.driverapplication.CommonUtilities;
import com.example.driverapplication.R;
import com.example.driverapplication.ServiceHandler;
import com.example.driverapplication.utilities.ConnectionDetector;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
public class LocationUpdate extends Service implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "DRIVER";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 30000;
private static final float LOCATION_DISTANCE = 0;
private double currentLat, currentLng;
private SharedPreferences pref;
private String driverId;
private GoogleApiClient mGoogleApiClient;
// A request to connect to Location Services
private LocationRequest mLocationRequest;
private LocationListener locationListener;
private class LocationListener implements
com.google.android.gms.location.LocationListener {
public LocationListener() {
}
@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "onLocationChanged: " + location);
currentLat = location.getLatitude();
currentLng = location.getLongitude();
}
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
boolean stopService = false;
if (intent != null)
stopService = intent.getBooleanExtra("stopservice", false);
System.out.println("stopservice " + stopService);
locationListener = new LocationListener();
if (stopService)
stopLocationUpdates();
else {
if (!mGoogleApiClient.isConnected())
mGoogleApiClient.connect();
}
return START_STICKY;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
pref = getSharedPreferences("driver_app", MODE_PRIVATE);
driverId = pref.getString("driver_id", "");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
}
public void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, locationListener);
if (mGoogleApiClient.isConnected())
mGoogleApiClient.disconnect();
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setInterval(35000);
mLocationRequest.setFastestInterval(30000);
startLocationUpates();
}
private void startLocationUpates() {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, locationListener);
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
}
}
这篇关于如何从IntentService获取FusedLocation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-03 11:07