google play service를 이용하여 위치정보를 받아오는데...
앱 종료후에도 화면 상단에 gps아이콘이 깜빡이면서 gps수신을 멈추질 않습니다.
소스 좀 봐주시면 감사할것 같습니다,^^
public class GetAddressTask extends AsyncTask<Location, Void, String>
{
@Override
protected String doInBackground(Location... params) {
// TODO Auto-generated method stub
Geocoder geocoder = new Geocoder(MainBluetoothActivity.this);
Location location = params[0];
String addrStr = null;
List<Address> addresses = null;
try {
addresses = (List<Address>)geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 5);
Address addr = addresses.get(0);
addrStr = addr.getAddressLine(0) + ", "+addr.getAdminArea() + ", "+addr.getCountryCode();
} catch (IOException e) {
e.printStackTrace();
}
return addrStr;
}
protected void onPostExecute(String result) {
place = result;
//Toast.makeText(getApplicationContext(), "Current Location : " + result, Toast.LENGTH_LONG).show();
//juso.setText(result);
}
}
@Override
public void onStart() {
super.onStart();
Log.e(TAG, "[main] onStart fired ..............");
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
updateUI();
mGoogleApiClient.connect();
}
@Override
public void onStop() {
super.onStop();
Log.e(TAG, "[main] onStop fired .........STOP GPS.....");
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
//Cast.CastApi.stopApplication(mGoogleApiClient);
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// TODO Auto-generated method stub
//Log.d(TAG, "Connection failed: " + connectionResult.toString());
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
//Log.d(TAG, "[Google services]onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
startLocationUpdates();
}
protected void startLocationUpdates() {
PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
//Log.d(TAG, "Location update started ..............: ");
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
// Log.d(TAG, "Location update stopped .......................");
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
//Log.d(TAG, "Firing onLocationChanged..............................................");
mCurrentLocation = location;
//mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
updateUI();
}
private void updateUI() {
//Log.d(TAG, "UI update initiated .............");
// 핸드폰에서 gps를 켜지않아 계속 null 값이 나온것 같다.ㅠㅠ
if (null != mCurrentLocation) {
String lat = String.valueOf(mCurrentLocation.getLatitude());
String lng = String.valueOf(mCurrentLocation.getLongitude());
//setup.setText("At Time: " + mLastUpdateTime + "\n" +
// "Latitude: " + lat + "\n" +
// "Longitude: " + lng + "\n" +
// "Accuracy: " + mCurrentLocation.getAccuracy() + "\n" +
// "Provider: " + mCurrentLocation.getProvider());
//Toast.makeText(MainBluetoothActivity.this, "update ui end", Toast.LENGTH_SHORT).show();
new GetAddressTask().execute(mCurrentLocation);
} else {
//Log.d(TAG, "location is null ...............");
}
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}