Опитвам се да разработя приложение за Android, което ще използва картата на Google и ще извлича местоположението на потребителя на всеки 5 минути и ще го показва на картата.
Имам три въпроса:
- Как да планирам услуга за намерение, така че местоположението да бъде извлечено за 5 минути?
- Моят код подходящ ли е за постигане на тази цел?
- Как мога да оптимизирам използването на батерията с то?
Благодаря
Ето моят код :
public class RetrieveActivity extends AppCompatActivity
implements
OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
GoogleMap.OnMyLocationButtonClickListener {
public static LatLng mCurrentLatLn = null;
MapFragment mMapFragment;
// The map used in MapFragment
private GoogleMap mGoogleMap;
private GoogleApiClient mGoogleApiClient;
private String KEY_CURRENT_LAT_LONG = "KEY_CURRENT_LAT_LONG";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("safer", "onCreate ");
// startService(); setContentView(R.layout.activity_main);
buildToolBar();
// Create a GoogleApiClient instance
buildGoogleApiClient();
mMapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mMapFragment.getMapAsync(this);
}
@Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
@Override
public void onPause() {
super.onPause();
Log.d("safer", "onPause ");
mGoogleApiClient.disconnect();
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mCurrentLatLn = savedInstanceState.getParcelable(KEY_CURRENT_LAT_LONG);
}
/**
* Utilitary method used to initialize our vars
*/
protected void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
private void buildToolBar() {
// Handle Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
// Set the toolbar title
TextView title = (TextView) toolbar.findViewById(R.id.toolbar_title);
title.setText(R.string.app_name);
title.setTextSize(28.0f);
// Set the toolbar navigation icon
toolbar.setNavigationIcon(R.drawable.ic_menu);
// Use the toolbar as Actionbar
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.setBuildingsEnabled(true);
mGoogleMap.setOnMyLocationButtonClickListener(this);
UiSettings uiSettings = mGoogleMap.getUiSettings();
uiSettings.setCompassEnabled(false);
uiSettings.setTiltGesturesEnabled(false);
uiSettings.setZoomControlsEnabled(true);
}
@Override
public void onConnected(Bundle bundle) {
Location l = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (l != null) {
mCurrentLatLn = new LatLng(l.getLatitude(), l.getLongitude());
moveCameraTo(mCurrentLatLn);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public boolean onMyLocationButtonClick() {
if (mCurrentLatLn != null) {
moveCameraTo(mCurrentLatLn);
} else {
Toast.makeText(RetrieveActivity.this, "Please enable location service...", Toast.LENGTH_SHORT).show();
}
return true;
}
// Method to start the service
public void startService() {
Log.d("safer", "startService ");
startService(new Intent(RetrieveActivity.this, FetchingData.class));
}
private void moveCameraTo(LatLng position) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(position)
.zoom(Constants.MAP_ZOOM_DEFAULT_LEVEL) // Sets the zoom
.tilt(Constants.MAP_TILT) // Sets the tilt of the camera to 30 degrees
.build();
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}