Android Location service - android

RESOLVED
Singleton pattern was suggested. Not the most elegant way to solve the problem but it works. Here is the code I used. Just have to get the instance in the main and LocationService class to use. See answer for how you are suppose to do it.
import android.location.Location;
public class LocationSingleton {
private Location location;
private static LocationSingleton singleton = new LocationSingleton();
private LocationSingleton(){
}
public static LocationSingleton getInstance( ) {
return singleton;
}
protected void setLocation(Location newLocation) {
this.location = newLocation;
}
protected Location getLocation(){
return this.location;
}
}
Attempting to use a background service to handle Location updates. I create a service in Main that updates the data (working correctly I believe) but I also have a button in main that whenever pressed does some work with the location data. I can't figure out though how to pass the Location from the service back to main. I've tried passing Main to the service but that didn't work and always created the service using the constructor without main passed. I've also tried creating a method in the service that returns the location but that kept getting a null pointer exception. How does one go about doing this.
Service Class
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
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;
import com.google.android.gms.location.LocationListener;
import java.util.Locale;
public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
private String TAG = "LocationService";
private GoogleApiClient mGoogleApiClient;
private static final long INTERVAL = 1000 * 15;
private static final long FATEST_INTERVAL = 1000 * 30;
private LocationRequest mLocationRequest;
private Location mCurrentLocation;
private Geocoder geocoder;
AddressStringOperations addressOps;
TimerUpdate timerUpdate;
Context mainContext;
MainActivity act;
public LocationService(MainActivity act) {
this.mainContext = mainContext;
Log.e(TAG, "Correct Constructor");
}
public LocationService(){
Log.e(TAG, "Shouldn't use");
}
#Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "onCreate");
this.geocoder = new Geocoder(this, Locale.getDefault());
addressOps = new AddressStringOperations(this.geocoder);
this.timerUpdate = new TimerUpdate(this, addressOps);
timerUpdate.startTimer();
mGoogleApiClient = new GoogleApiClient.Builder(LocationService.this)
.addApi(LocationServices.API).addConnectionCallbacks(LocationService.this)
.addOnConnectionFailedListener(LocationService.this).build();
mGoogleApiClient.connect();
createLocationRequest();
}
#Override
public void onDestroy(){
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId){
Log.e(TAG, "Service Started");
return super.onStartCommand(intent, flags, startId);
}
protected void createLocationRequest(){
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FATEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
public Location getLocation(){
Log.e(TAG, "Latitude: "+ mCurrentLocation.getLatitude() + "\n" + "Longitude: " + mCurrentLocation.getLatitude());
return this.mCurrentLocation;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onConnected(Bundle bundle) {
Log.e(TAG, "Connection Successful");
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
#Override
public void onConnectionSuspended(int i) {
Log.e(TAG, "Connection Lost");
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG, "Connection Failed");
}
#Override
public void onLocationChanged(Location location) {
Log.e(TAG, "Firing onLocationChanged.........");
//Log.e(TAG, "Latitude: "+ location.getLatitude() + "\n" + "Longitude: " + location.getLatitude());
timerUpdate.location = location;
mCurrentLocation = location;
}
}
Main Class
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.location.Geocoder;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import java.util.Locale;
public class MainActivity extends Activity {
private Button bLogout, bWebsite;
private ImageButton bLogData;
private TextView etLabel;
private UserLocalStore userLocalStore;
private static final String TAG = "MainActivity";
TimerUpdate timerUpdate;
Geocoder geocoder;
Location location;
AddressStringOperations addressOps;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e(TAG, "On Create . . . . .");
if(!isGooglePlayServicesAvailable()){
startActivity(new Intent(MainActivity.this, login.class));
Log.e(TAG, "No GooglePlayServices");
finish();
Toast.makeText(getApplicationContext(), "Please update GooglePlay Servies to use this Application", Toast.LENGTH_LONG).show();
}else{
userLocalStore = new UserLocalStore(this);
this.geocoder = new Geocoder(this, Locale.getDefault());
addressOps = new AddressStringOperations(this.geocoder);
this.timerUpdate = new TimerUpdate(this, addressOps, false);
if (authenticate() != true) {
startActivity(new Intent(MainActivity.this, login.class));
Log.e(TAG, "Authenticate is not true");
finish();
} else {
etLabel = (TextView) findViewById(R.id.etEmailLabel);
bLogout = (Button) findViewById(R.id.bLogout);
bLogData = (ImageButton) findViewById(R.id.DataLog);
bWebsite = (Button) findViewById(R.id.website);
LocationService service = new LocationService(MainActivity.this);
Intent start = new Intent(MainActivity.this, LocationService.class);
MainActivity.this.startService(start);
bLogData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String pressStatus = "3";
Log.e(TAG, "Latitude: "+ location.getLatitude() + "\n" + "Longitude: " + location.getLatitude());
timerUpdate.update(pressStatus);
}
});
bLogout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
userLocalStore.clearuserData();
userLocalStore.setUserLoggedIn(false);
startActivity(new Intent(MainActivity.this, login.class));
finish();
}
});
bWebsite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("temp"));
startActivity(browserIntent);
}
});
}
}
}
public void setLocation(Location newLocation){
this.location = newLocation;
}
}

You can do that in multiple ways. You can create a Receiver and broadcast and intent from there or you can use an EventBus ( https://github.com/greenrobot/EventBus ).
In this case I think it's more appropriate the Service/Receiver model, you can find here an example: http://www.truiton.com/2014/09/android-service-broadcastreceiver-example/
If you need it only in one activity, consider to use an AsyncTask and handle the output in the postExecute method, it would be a lot easier.

Related

Activity doesnt mantain connection to its service after minimalising it

I have a problem with an activity after minimalising. Everything is going ok when i start an activity and press start button. But when i minimalise activity and again maximalize it, it doesnt respond to my buttons and commands. Anybody know what to do? This is my first android app so i dont know what is going on..
here are my classes :
TrackerService
package sk.tuke.smart.makac.services;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
public class TrackerService extends Service implements LocationListener {
private Intent commandIntent;
private long duration;
private boolean paused,checkedAfterPause;
private int sportActivity;
private double distance,pace,calories;
private ArrayList<Location> finalPositionList = new ArrayList<Location>();
private LocationManager locationManager;
private static final String TAG = "TrackerService";
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i(TAG,"onStart intent " +intent.getAction());
commandIntent=intent;
checkedAfterPause=true;
if(intent.getAction() == "sk.tuke.smart.makac.COMMAND_START"){
if(intent.getAction() == "sk.tuke.smart.makac.COMMAND_START") {
duration = 0;
}
new Timer().scheduleAtFixedRate(new TimerTask()
{
#Override
public void run() {
if(!paused){
duration++;
Intent intent1 = new Intent();
intent1.setAction("sk.tuke.smart.makac.TICK");
intent1.putExtra("duration", duration);
intent1.putExtra("distance",distance);
sendBroadcast(intent1);
Log.i(TAG,"" + duration);
}
}
}, 1000, 1000);
}else if (intent.getAction() == "sk.tuke.smart.makac.COMMAND_PAUSE"){
paused=true;
locationManager.removeUpdates(this);
}
if(intent.getAction() == "sk.tuke.smart.makac.COMMAND_CONTINUE"){
paused=false;
checkedAfterPause=false;
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,10,this);
}
}
#Override
public void onCreate() {
super.onCreate();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,10,this);
}
#Override
public void onDestroy() {
locationManager.removeUpdates(this);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onLocationChanged(Location location) {
finalPositionList.add(location);
Location lastLocation;
double minDistance;
if (finalPositionList.size() != 1 && checkedAfterPause) {
lastLocation = finalPositionList.get(finalPositionList.size() - 2);
minDistance=location.distanceTo(lastLocation);
if(minDistance>=2){
distance += location.distanceTo(lastLocation);
}else{
finalPositionList.remove(finalPositionList.size()-1);
}
}
if(commandIntent.getAction() == "sk.tuke.smart.makac.COMMAND_CONTINUE" && !checkedAfterPause){
Log.i(TAG,"checking distance after pause");
lastLocation = finalPositionList.get(finalPositionList.size() - 2);
minDistance=location.distanceTo(lastLocation);
if(minDistance<=100){
distance += location.distanceTo(lastLocation);
}
checkedAfterPause=true;
}
Log.i(TAG,"locations " + finalPositionList);
Log.i(TAG,"distance = " + distance);
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
public long getDuration(){
return duration;
}
public double getPace(){
return pace;
}
}
SportsActivity
package sk.tuke.smart.makac;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import sk.tuke.smart.makac.helpers.MainHelper;
import sk.tuke.smart.makac.services.TrackerService;
public class StopwatchActivity extends AppCompatActivity {
private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 111;
private static final String TAG = "StopwatchActivity";
private boolean started;
private boolean running;
private boolean paused=false;
private long duration;
private double distance;
private MainHelper helper;
private TextView durationView,distanceView;
private Button startButton,endButton;
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction()=="sk.tuke.smart.makac.TICK"){
duration = intent.getLongExtra("duration",duration);
distance = intent.getDoubleExtra("distance",distance);
durationView.setText(helper.formatDuration(duration));
distanceView.setText(helper.formatDistance(distance));
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stopwatch);
IntentFilter intentFilter = new IntentFilter("sk.tuke.smart.makac.TICK");
registerReceiver(receiver,intentFilter);
started=false;
running=false;
helper = new MainHelper();
durationView = findViewById(R.id.textview_stopwatch_duration);
distanceView = findViewById(R.id.textview_stopwatch_distance);
startButton = findViewById(R.id.button_stopwatch_start);
endButton = findViewById(R.id.button_stopwatch_endworkout);
if(!canAccessLocation()){
ActivityCompat.requestPermissions(this,
new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_FINE_LOCATION);
}
}
public void toggle(View view){
Intent intent = new Intent(this,TrackerService.class);
started=true;
this.running = !this.running;
if(running && started){
startButton.setText("Stop");
endButton.setVisibility(view.GONE);
if(paused){
intent.setAction("sk.tuke.smart.makac.COMMAND_CONTINUE");
paused=false;
}else
intent.setAction("sk.tuke.smart.makac.COMMAND_START");
}
if(!running && started){
startButton.setText("Continue");
endButton.setVisibility(view.VISIBLE);
intent.setAction("sk.tuke.smart.makac.COMMAND_PAUSE");
paused=true;
}
startService(intent);
}
public void endWorkout(View view){
Intent intent = new Intent(this,TrackerService.class);
intent.setAction("sk.tuke.smart.makac.COMMAND_STOP");
startService(intent);
setContentView(R.layout.activity_workout_detail);
onStop();
}
public void openMaps(View view){
Intent intent = new Intent(StopwatchActivity.this, MapsActivity.class);
startActivity(intent);
}
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onStop() {
super.onStop();
Intent intent= new Intent(this,TrackerService.class);
stopService(intent);
}
private boolean hasPermission(String perm) {
return(PackageManager.PERMISSION_GRANTED==checkSelfPermission(perm));
}
private boolean canAccessLocation() {
return(hasPermission(Manifest.permission.ACCESS_FINE_LOCATION));
}
}
You aren't creating a connection to the service. You're only starting it, not binding it. So there's no connection to maintain.
It looks like you're trying to do quasi-binding via actions. Don't do that, properly bind the service and avoid a whole raft of problems like this.

GoogleFusedLocation api returns me null for lat and 0.0 for lon and altitude ever

Sorry for the long title but couldn't resume it more, basicly i implemented the googlefusedlocation api as a service, i need the service to run on background and update the location, this service starts on my app running (atm i am not requesting the user if he want to share location because i have no idea how ti implement it with a service).
My service:
package com.example.afcosta.inesctec.pt.android.services;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
public class GoogleLocation extends Service implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private static final String TAG = "BOOMBOOMTESTGPS";
private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = 0;
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 0;
private static final float LOCATION_DISTANCE = 0f;
private int updatePriority;
private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
private final IBinder mBinder = new LocalBinder();
private Intent intent;
private String provider;
Context context;
Location mLastLocation;
public class LocalBinder extends Binder {
public GoogleLocation getServerInstance() {
return GoogleLocation.this;
}
}
public Location getLocation() {
Log.d("IMHERE", "HELLO");
return mLastLocation;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
context = getApplicationContext();
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
#Override
public void onCreate() {
Log.e(TAG, "onCreate");
initializeLocationManager();
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
this.updatePriority = LocationRequest.PRIORITY_HIGH_ACCURACY;
} else if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
this.updatePriority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY;
} else {
this.updatePriority = LocationRequest.PRIORITY_HIGH_ACCURACY;
}
this.buildGoogleApiClient();
this.createLocationRequest();
this.googleApiClient.connect();
}
#Override
public void onConnected(#Nullable Bundle bundle) {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(this.googleApiClient, this.locationRequest,this);
} else {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
}
#Override
public void onLocationChanged(Location location) {
mLastLocation = location;
}
#Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
this.googleApiClient.unregisterConnectionCallbacks(this);
this.googleApiClient.unregisterConnectionFailedListener(this);
this.googleApiClient.disconnect();
this.mLastLocation = null;
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
private synchronized void buildGoogleApiClient() {
this.googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
private void createLocationRequest() {
this.locationRequest = new LocationRequest();
this.locationRequest.setInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
this.locationRequest.setPriority(updatePriority);
}
then on my camera i access the getLocation method that is inside my service, it gives me the last location, i just display the way i acces the service if you guys ask, but i just remember that i had a solution before(i used locationListener and not google one) and i could get the location, failled sometimes thats why i tried to change.
public void onServiceConnected(ComponentName name, IBinder service) {
mBounded = true;
GoogleLocation.LocalBinder mLocalBinder = (GoogleLocation.LocalBinder)service;
mlocation = mLocalBinder.getServerInstance();
location = mlocation.getLocation();
Log.d("localizacao",location.toString());
}
Now i get:
lat:null
lon:0.0
alt:0.0
Strange, why is that happening?
Thanks

Location only updating TextView when app is out of focus

For some reason it only updates the textviews when the app hits onPause, like when I hit the home button, or multitasking button. Can someone help me figure out why that is?
MainActivity.java:
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private String lat, lon;
private TextView longTextView, latTextView;
LocationService locationService = new LocationService(this);
private Intent intentService;
private PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latTextView = (TextView) findViewById(R.id.latitude_textview);
longTextView = (TextView) findViewById(R.id.longitude_textview);
}
#Override
protected void onStart() {
super.onStart();
locationService.buildGoogleApiClient();
locationService.apiConnect();
if (latTextView != null && longTextView != null) {
latTextView.setText( locationService.getLat());
longTextView.setText( locationService.getLon());
Toast.makeText(getApplicationContext(), " Actually got location", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(getApplicationContext(), "The shit was null fam", Toast.LENGTH_LONG)
.show();
}
}
#Override
protected void onStop() {
super.onStop();
locationService.apiDisconnect();
}
}
LocationService.java:
import android.Manifest;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.FusedLocationProviderApi;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import static com.google.android.gms.wearable.DataMap.TAG;
public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
// ============================================================= Variables
Context context;
Location mLastLocation;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private String lat, lon;
final static String[] LOCATION_PERMISSIONS = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10000;
public static final long UPDATE_FASTEST_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
public static boolean isEnded = false;
public static Boolean requestingLocationUpdates;
protected String lastUpdateTime;
final int GOOGLEAPI_REQUEST_CODE = 24;
private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
// ============================================================= Constructor
public LocationService(Context context) {
this.context = context;
}
// ============================================================= Getters / Setters
public String getLon() {
return lon;
}
public void setLon(String lon) {
this.lon = lon;
}
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
// ============================================================= Methods
synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
public void apiConnect() {
mGoogleApiClient.connect();
}
public void apiDisconnect() {
mGoogleApiClient.disconnect();
}
void updateUI() {
}
// ============================================================= Implemented Location Methods
#Override
public void onLocationChanged(Location location) {
setLat(String.valueOf(location.getLatitude()));
setLon(String.valueOf(location.getLongitude()));
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + connectionResult.getErrorCode());
}
#Override
public void onConnected(#Nullable Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS); // Sets Location to update every second
mLocationRequest.setFastestInterval(UPDATE_FASTEST_INTERVAL_IN_MILLISECONDS); // The fastest location can update is every half-second
startLocationUpdates();
// TODO come back to this to see whats up
/* mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);*/
if (mLastLocation != null) {
setLat(String.valueOf(mLastLocation.getLatitude()));
setLon(String.valueOf(mLastLocation.getLongitude()));
}
}
#Override
public void onConnectionSuspended(int i) {
}
protected void startLocationUpdates() {
/*if (!requestingLocationUpdates) {
requestingLocationUpdates = true;*/
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) context, LOCATION_PERMISSIONS, GOOGLEAPI_REQUEST_CODE);
} else {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
Log.i(TAG, " startLocationUpdates===");
isEnded = true;
//}
}
// ============================================================= Implemented Service Methods
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Within {#code onPause()}, we pause location updates, but leave the
// connection to GoogleApiClient intact. Here, we resume receiving
// location updates if the user has requested them.
Log.d("LOC", "Service init...");
isEnded = false;
requestingLocationUpdates = false;
lastUpdateTime = "";
buildGoogleApiClient();
if (mGoogleApiClient.isConnected() && requestingLocationUpdates) {
startLocationUpdates();
}
return Service.START_REDELIVER_INTENT;
}
}
The reason why you are not getting the location updated in textview is because your code doesn't have a way for the service to communicate back to the activity.
If you want to obtain location only when the activity is in foreground don't use Service and please look into this google's example for obtaining location and updating on a TextView using fused location provider.
I am not sure why you are using a Service.Use Service only when you want to continuously fetch the location even when the app is running background.
For this use any one of the method mentioned here to inform the activity that a new location has been obtained.LocalBroadcast would be your best bet. Anyway explore the best possible solution that suits your usecase in the previous link.

Tracking Service won't work after uprading program's Android API

My professor has a program that I am trying to help hime upgrade, unfortunately I am extremely new to Android. It a program that is supposed to grab your Android's location and periodically send it to a server (about every 5 seconds). But after upgrading it to use API 21 (it was on 10-14), it will only return my location as 0.0. Does anyone know what might have caused this?
Here is the Main
import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.location.LocationManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
protected Button panic;
protected Button track;
private TextView latText;
private TextView lngText;
private TextView addressText;
private boolean panicking;
private boolean tracking;
protected Context mContext;
private Timer locTimer;
private final String TAG = "MainActivity";
protected Sender send;
private int trackingOn;
private int trackingOff;
private int panicOn;
private int panicOff;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tracking = false;
panicking = false;
//colors
trackingOff = Color.rgb(100, 255, 100);
trackingOn = Color.rgb(255, 255, 100);
panicOff = Color.rgb(255, 50, 50);
panicOn = Color.rgb(255, 127, 0);
setupSender();
setupView();
//createGPS();
checkGPS();
//create GPS service
Intent trackIntent = new Intent(MainActivity.this, TrackingService.class);
startService(trackIntent);
createTimer();
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onDestroy()
{
locTimer.cancel();
super.onDestroy();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
Log.d(TAG, "Back pressed");
//create dialog to turn off GPS
//if not then ask the user to turn it on
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Would you like to turn off the GPS?");
builder.setCancelable(false);
//yes
builder.setPositiveButton("Disable GPS in device settings", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
finish();
}
});
//no
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
finish();
}
});
AlertDialog gpsAlert = builder.create();
gpsAlert.show();
}
return super.onKeyDown(keyCode, event);
}
private void setupSender()
{
//get MAC address
WifiManager wifiMan = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
String macAddr = wifiInf.getMacAddress();
//get phone Number
TelephonyManager teleMan = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = teleMan.getLine1Number();
Log.d(TAG, "My mac: " + macAddr + " My number: " + phoneNumber);
send = new Sender(macAddr, phoneNumber);
}
private class SendTracking extends AsyncTask<String, String, String>
{
#Override
protected String doInBackground(String... arg0)
{
send.sendTrack(Values.lat, Values.lng, Values.myAddress, Values.acc);
return null;
}
}
private class SendPanic extends AsyncTask<String, String, String>
{
#Override
protected String doInBackground(String... arg0)
{
send.sendPanic(Values.lat, Values.lng, Values.myAddress, Values.acc);
return null;
}
}
private void setupView()
{
latText = (TextView)findViewById(R.id.lattitude);
lngText = (TextView)findViewById(R.id.longitude);
addressText = (TextView)findViewById(R.id.address);
// create tracking button
track = (Button)findViewById(R.id.trackMe);
track.setBackgroundColor(trackingOff);
track.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
tracking = !tracking;
Log.w(TAG, "Tracking button pressed");
if(tracking)
{
track.setBackgroundColor(trackingOn);
track.setText(getResources().getString(R.string.stopTrack));
Values.interval = Values.TRACK_INTERVAL; //set it so it sends right away
}
else
{
new SendTracking().execute();
track.setBackgroundColor(trackingOff);
track.setText(getResources().getString(R.string.tracker));
}
}
});
// create panic button
panic = (Button) findViewById(R.id.panic);
panic.setBackgroundColor(panicOff);
panic.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
Log.w(TAG, "Panic Pressed");
panicking = !panicking;
//send.sendPanic(Values.lat, Values.lng, Values.myAddress, Values.acc);
if(panicking)
{
panic.setBackgroundColor(panicOn);
panic.setText(getResources().getString(R.string.stopPanic));
Values.interval = Values.TRACK_INTERVAL; //set it so it sends right away
}
else
{
panic.setBackgroundColor(panicOff);
panic.setText(getResources().getString(R.string.panic));
}
}
});
}
private void createTimer()
{
//decimal formatter
final DecimalFormat format = new DecimalFormat("#.###");
// create runnable for timer
final Runnable updateLoc = new Runnable()
{
#Override
public void run()
{
Log.i(TAG, "Updating loc");
latText.setText("" + format.format(Values.lat));
lngText.setText("" + format.format(Values.lng));
addressText.setText(Values.myAddress);
//send to DB if tracking is on and enough time has passed since last update
if(tracking)
{
if (Values.interval >= Values.TRACK_INTERVAL)
{
new SendTracking().execute();
Values.interval = 0;
}
else
{
Values.interval++;
}
}
else if(panicking)
{
if (Values.interval >= Values.TRACK_INTERVAL)
{
new SendPanic().execute();
Values.interval = 0;
}
else
{
Values.interval++;
}
}
}
};
// create timer that will check for location
locTimer = new Timer("LocTimer");
locTimer.scheduleAtFixedRate(new TimerTask()
{
#Override
public void run()
{
runOnUiThread(updateLoc);
}
}, Values.UPDATE_TIMER / 2, Values.UPDATE_TIMER);
}
private void checkGPS()
{
LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//check to see if GPS is enabled
if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
//if not then ask the user to turn it on
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("GPS is currently disabled. Please turn it on");
builder.setCancelable(false);
//yes
builder.setPositiveButton("Enabled GPS in device settings", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
}
});
//no
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
AlertDialog gpsAlert = builder.create();
gpsAlert.show();
}
}
}
And this is the Tracking Service
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.Manifest;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
public class TrackingService extends Service {
private LocationManager manager;
private LocationListener listener;
private final String TAG = "TrackingService";
public class LocalBinder extends Binder {
TrackingService getService() {
return TrackingService.this;
}
}
public IBinder onBind(Intent arg0) {
return new LocalBinder();
}
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
public void onStart(Intent intent, int startId) {
Log.d(TAG, "Starting service");
this.onStart(intent, startId);
}
public void onCreate() {
Log.d(TAG, "Creating service");
this.createGPS();
}
public void onDestroy() {
Log.d(TAG, "Killing service");
manager.removeUpdates(listener);
super.onDestroy();
}
private LocationManager createGPS() {
// create GPS service
manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//context
final Context context = this;
// make listener
listener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
Log.d(TAG, "GPS udpate");
Values.lat = location.getLatitude();
Values.lng = location.getLongitude();
Values.acc = location.getAccuracy();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Geocoder.isPresent()) {
// Since the geocoding API is synchronous and may take a while. You don't want to lock
// up the UI thread. Invoking reverse geocoding in an AsyncTask.
(new ReverseGeocodingTask(context)).execute(new Location[]{location});
}
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
// register listener
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, Values.UPDATE_TIMER, 10f, listener);
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, Values.UPDATE_TIMER, 10f, listener);
return manager;
}
// AsyncTask encapsulating the reverse-geocoding API. Since the geocoder API is blocked,
// we do not want to invoke it from the UI thread.
private class ReverseGeocodingTask extends AsyncTask<Location, Void, Void>
{
Context mContext;
public ReverseGeocodingTask(Context context)
{
super();
mContext = context;
}
#Override
protected Void doInBackground(Location... params)
{
Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
Location loc = params[0];
List<Address> addresses = null;
try
{
// Call the synchronous getFromLocation() method by passing in the lat/long values.
addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
}
catch (IOException e)
{
e.printStackTrace();
// Update UI field with the exception.
//Message.obtain(mHandler, UPDATE_ADDRESS, e.toString()).sendToTarget();
Log.e("ReverseGeoCoder", "error: " + e.toString());
}
if (addresses != null && addresses.size() > 0)
{
Address address = addresses.get(0);
// Format the first line of address (if available), city, and country name.
String addressText = String.format("%s, %s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getLocality(),
address.getCountryName());
// Update the UI via a message handler.
//Message.obtain(mHandler, UPDATE_ADDRESS, addressText).sendToTarget();
Log.d("ReverseGeoCoder", "address: " + addressText);
Values.myAddress = addressText;
}
return null;
}
}
}
It worked just fine before, I've seen the tracking logs, but now it only returns 0.0.

LocationManager.requestLocationUpdates is never called

I have an application and I want to use the GPS's Location. After implementing what I was taught, I have two files, VirtualAssistantActivity which uses the LocationGPS file. The second file will send the result with an intent received from the first file.
The problem is the TextView that is supposed to be edited after receiving the intent filled with the location.toString() doesn't change. After using the debug feature of Android Studio, I've found that it's like the locationManager's requestLocationUpdate is never called, I can't even get into the new LocationListener's creation.
I am using a Sony XPERIA running Android 5.0, and decided to develop targeting Android 4.0 with API 15.
Here's the VirtualAssistantActivity code :
public class VirtualAssistantActivity extends AppCompatActivity {
private LocationGPS lgps = null;
private BroadcastReceiver mLocationReceiver;
private TextView tvTest = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_virtual_assistant);
tvTest = (TextView) findViewById(R.id.tV_testGeo);
lgps = LocationGPS.get(this.getApplicationContext());
mLocationReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent){
Location l = (Location)intent.getParcelableExtra(LocationManager.KEY_LOCATION_CHANGED);
if (l!=null){
tvTest.setText(""+l);
} else {
tvTest.setText("Empty intent");
}
}
};
}#Override
protected void onResume(){
super.onResume();
this.registerReceiver(mLocationReceiver, new IntentFilter(LocationGPS.ACTION_LOCATION));
lgps.startLocationUpdates();
Toast.makeText(getApplicationContext(), "BR Init", Toast.LENGTH_SHORT).show();
}
#Override
public void onPause() {
super.onPause();
//unregisterReceiver(mLocationReceiver);
}
And the LocationGPS's :
public class LocationGPS {
static LocationGPS slocationGPS = null;
Context mAppContext = null;
LocationManager mLocationManager = null;
public static String ACTION_LOCATION = "LOCATION_MSG";
private LocationGPS(Context appContext) {
mAppContext = appContext;
mLocationManager = (LocationManager)appContext.getSystemService(Context.LOCATION_SERVICE);
}
public static LocationGPS get(Context c) {
if (slocationGPS == null) {
slocationGPS = new LocationGPS(c);
}
return slocationGPS;
}
private void broadcastLocation(Location location) {
Intent broadcast = new Intent(this.ACTION_LOCATION);
broadcast.putExtra(LocationManager.KEY_LOCATION_CHANGED, location);
this.mAppContext.sendBroadcast(broadcast);
}
public void startLocationUpdates(){
if (ContextCompat.checkSelfPermission(mAppContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(mAppContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocationManager.requestLocationUpdates
(LocationManager.GPS_PROVIDER, 1000, 5, new LocationListener() {
#Override
public void onLocationChanged(Location location) {
if (location != null) {
//Log.d(this.getClass().getName(), location.toString());
Log.d("GPS", "Latitude " + location.getLatitude() + " et longitude " + location.getLongitude());
Toast.makeText(mAppContext, "Location", Toast.LENGTH_SHORT).show();
broadcastLocation(location);
}
Toast.makeText(mAppContext, "Location", Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
});
}
I've searched for some solutions, and while it seems to work for everyone, or at least they have some error messages, I don't know what I can do except eventually use Google's APIs, but I would prefer to avoid that. I have checked my permission, they're granted and this is a list of my imports:
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;
And the permissions :
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Categories

Resources