How to communicate with continuously running background service in Activity? - android

In my project, there is a service that retrieves location of users. It continuously runs in background. I want to set location on my textview.
So, I have ManinActivity as follows:
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends Activity {
GPSTracker gps;
double mylatitude, mylongitude;
TextView txt_location;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_location = (TextView)findViewById(R.id.current_location);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
public void btn_remember_Clicked(View v)
{
Intent i = new Intent(MainActivity.this,RememberActivity.class);
startActivity(i);
}
public void btn_show_places_Clicked(View v)
{
Intent i = new Intent(MainActivity.this,ShowPlacesActivity.class);
startActivity(i);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
}
And my LocationService class is as follows:
import android.app.Service;
import android.content.Context;
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 android.widget.Toast;
public class LocationService extends Service
{
public static final String BROADCAST_ACTION = "Hello World";
private static final int TWO_MINUTES = 1000 * 60 * 2;
public LocationManager locationManager;
public MyLocationListener listener;
public Location previousBestLocation = null;
Intent intent;
#Override
public void onCreate()
{
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
}
#Override
public int onStartCommand(Intent intent,int flags, int startId)
{
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
return super.onStartCommand(intent, flags, startId);
}
#Override
public IBinder onBind(Intent intent)
{
return null;
}
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be worse
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return true;
}
return false;
}
/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
#Override
public void onDestroy() {
// handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
Log.v("STOP_SERVICE", "DONE");
locationManager.removeUpdates(listener);
}
public static Thread performOnBackgroundThread(final Runnable runnable) {
final Thread t = new Thread() {
#Override
public void run() {
try {
runnable.run();
} catch(Exception e) {
System.out.print(e);
}
}
};
t.start();
return t;
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(final Location loc) {
Log.i("**************************************", "Location changed");
if(isBetterLocation(loc, previousBestLocation)) {
loc.getLatitude();
loc.getLongitude();
intent.putExtra("Latitude", loc.getLatitude());
intent.putExtra("Longitude", loc.getLongitude());
intent.putExtra("Provider", loc.getProvider());
sendBroadcast(intent);
}
}
public void onProviderDisabled(String provider) {
Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
and I have LocationBroadcast class as follows:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class LocationBroadcast extends BroadcastReceiver{
#Override
public void onReceive(Context ctx, Intent intent) {
ctx.startService(new Intent(ctx, LocationService.class));
}
}
I want to set location on textview txt_location in MainActivity. I do not know much about services and how we can communicate within Activity. How can I get latitude and longitude in MainActivity? Please help.
Update:
I have just updated my code according to answers:
public class LocationService extends Service
{
private static final int TWO_MINUTES = 1000 * 60 * 2;
public LocationManager locationManager;
public MyLocationListener listener;
public Location previousBestLocation = null;
Intent i;
#Override
public void onCreate()
{
super.onCreate();
i = new Intent("LOCATION_CHANGED");
}
#Override
public int onStartCommand(Intent intent,int flags, int startId)
{
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
return super.onStartCommand(intent, flags, startId);
}
#Override
public IBinder onBind(Intent intent)
{
return null;
}
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be worse
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return true;
}
return false;
}
/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
#Override
public void onDestroy() {
// handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
Log.v("STOP_SERVICE", "DONE");
locationManager.removeUpdates(listener);
}
public static Thread performOnBackgroundThread(final Runnable runnable) {
final Thread t = new Thread() {
#Override
public void run() {
try {
runnable.run();
} catch(Exception e) {
System.out.print(e);
}
}
};
t.start();
return t;
}
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(final Location loc)
{
Log.i("**************************************", "Location changed");
if(isBetterLocation(loc, previousBestLocation)) {
String newLocation = "Lat: " + loc.getLatitude() + " and Long: " + loc.getLongitude();
i = new Intent("LOCATION_CHANGED");
i.putExtra("location", newLocation);
sendBroadcast(i);
}
}
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider)
{
Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
}
And I have received broadcasted intent in MainActivity as follows:
public class MainActivity extends Activity {
TextView txt_location;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_location = (TextView)findViewById(R.id.current_location);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public class LocationBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent intent) {
txt_location.setText(intent.getExtras().getString("location"));
}
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
}
As you can see, it is not showing any location updates. Logcat isn't showing any errors. Can anyone please guide me what is the mistake?

Create a BroadcastReceiver in the MainActivity like
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context appContext, Intent intent) {
.......
//get location from Service and create Method for set location on textview txt_location
}
};
registerReceiver in onResume of MainActivity
registerReceiver(broadcastReceiver, new IntentFilter(GET_LOCATION));
and declare this in starting MainActivity
private static final String GET_LOCATION = "com.pkg.location";
and in your service pass your location as a broadcase
Intent intent = new Intent(GET_LOCATION);
intent.putExtra("location", "your_location_got_from_service");
sendBroadcast(intent);
and dont forget to unregisterReceiver in MainActivity
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
}

You can use BroadcastReceiver to receive the event of your LOCATION_CHANGED, and update the TextView from its onReceive method:
In your activity:
TextView textView = (TextView) findViewById(R.id.location_text); //global variable
registerReceiver(locationChangedReceiver, new IntentFilter("LOCATION_CHANGED"));
//your receiver class as inner class of activity.
private BroadcastReceiver locationChangedReceiver= new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
textView.setText(intent.getExtras().getString("location"));
}
};
In your service, you can simply broadcast for "LOCATION_CHANGED", and pass String location as intent's extra argument.
String newLocation = "Lat: " + loc.getLatitude() + " and Long: " + loc.getLongitude();
Intent i = new Intent("LOCATION_CHANGED");
i.putExtra("location", newLocation);
sendBroadcast(i);
If you want to keep a separate receiver class (not in activity), you can use your context to update views like this:
public class LocationBroadcast extends BroadcastReceiver{
#Override
public void onReceive(Context ctx, Intent intent) {
TextView textView = (TextView) ctx.findViewById(R.id.location_text);
textView.setText(intent.getExtras().getString("location"));
}
}
Hope it helps.

Related

Android LocationListener not called

I'm making an app that sends the present location through sms. But the location listener method is not called. It says:
method LocationListener is never called
Here's my code :
public class MainActivity2 extends Activity {
LocationManager lm;
LocationListener locationListener;
private void LocationListener(Context context) {
lm = (LocationManager)
context.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
60000,
1000,
locationListener);
}
private class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
if (loc != null) {
String Uri = "http://maps.google.com/maps?saddr=" + loc.getLatitude() + "," + loc.getLongitude();
SmsManager smsManager = SmsManager.getDefault();
StringBuffer smsBody = new StringBuffer();
smsBody.append(Uri);
smsManager.sendTextMessage("+91847690****", null, smsBody.toString(), null, null);
lm.removeUpdates(locationListener);
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
It looks like the confusion is that you have created a method in your MainActivity2 class that is called LocationListener(). This is not good, since you shouldn't have a method with the same name as the LocationListener class.
Just rename the method, and since this method is part of your Activity, you don't need to pass in a Context:
private void setUpLocationListener() {
lm = (LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
60000,
1000,
locationListener);
}
Then, just call the method from onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
setUpLocationListener();
}

Getting current location continuously in background

In my project, i have used a service to retrieving location continuously in background. My LocationService is as follows:
public class LocationService extends Service
{
private static final int TWO_MINUTES = 1000 * 60 * 2;
public LocationManager locationManager;
public MyLocationListener listener;
public Location previousBestLocation = null;
Intent i;
#Override
public void onCreate()
{
super.onCreate();
i = new Intent("LOCATION_CHANGED");
}
#Override
public int onStartCommand(Intent intent,int flags, int startId)
{
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
return super.onStartCommand(intent, flags, startId);
}
#Override
public IBinder onBind(Intent intent)
{
return null;
}
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be worse
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return true;
}
return false;
}
/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
#Override
public void onDestroy() {
// handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
Log.v("STOP_SERVICE", "DONE");
locationManager.removeUpdates(listener);
}
public static Thread performOnBackgroundThread(final Runnable runnable) {
final Thread t = new Thread() {
#Override
public void run() {
try {
runnable.run();
} catch(Exception e) {
System.out.print(e);
}
}
};
t.start();
return t;
}
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(final Location loc)
{
Log.i("**************************************", "Location changed");
if(isBetterLocation(loc, previousBestLocation)) {
String newLocation = "Lat: " + loc.getLatitude() + " and Long: " + loc.getLongitude();
i = new Intent("LOCATION_CHANGED");
i.putExtra("location", newLocation);
sendBroadcast(i);
}
}
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider)
{
Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
}
And I have received broadcasted intent in MainActivity as follows:
public class MainActivity extends Activity {
TextView txt_location;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_location = (TextView)findViewById(R.id.current_location);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public class LocationBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent intent) {
txt_location.setText(intent.getExtras().getString("location"));
}
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container,
false);
}
}
}
All i want to do is to set location's latitude and longitude on txt_location. this code isn't working and showing txt_location as it is:
As you can see, it is not showing any location updates...Logcat isn't showing any errors..Can anyone please guide me what is the mistake?
Use this class as Service. but before this define service class in manifest
<service android:name="com.example.androidservice.beckend.EndlessService" />
and use in activity class
startService(new Intent ( this , EndlessService.class)) ;
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.util.Log;
import android.widget.Toast;
public class EndlessService extends Service {
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
// Handler that receives messages from the thread
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
#Override
public void handleMessage(Message msg)
{
new HitToTheInternet().execute("");
}
}
#Override
public void onCreate() {
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block. We also make it
// background priority so CPU-intensive work will not disrupt our UI.
HandlerThread thread = new HandlerThread("ServiceStartArguments",Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
// Get the HandlerThread's Looper and use it for our Handler
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
// For each start request, send a message to start a job and deliver the
// start ID so we know which request we're stopping when we finish the
// job
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);
if ( intent == null )
{
Log.e("intentStatus", "intent is null");
}
// If we get killed, after returning from here, restart
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}
#Override
public void onDestroy() {
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
class HitToTheInternet extends AsyncTask<String, String, String>
{
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
Log.e("doInBackground", "Running") ;
return null;
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
new Handler().postDelayed(new Runnable() {
public void run() {
Message msg = mServiceHandler.obtainMessage();
mServiceHandler.sendMessage(msg);
}
}, 10000);
}
}
}
for starting service says at 5 seconds:
AlarmManager alarm =(AlarmManager)context.getSystemService(context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context,MonitorService.class), 0);
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 5*1000, pi);
For stopping alarm:
AlarmManager mgr =(AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(this,MonitorService.class), 0);
mgr.cancel(pi);
pi.cancel();

Start a service when activity is paused

i'm creating an app that show user's location into a map, now i want to update the user location when the app is in background,especially i want to start the Service when the Activity's method onPause() is called.
my activity:
public class MapsActivity extends Activity implements OnClickListener, GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, LocationListener, OnInfoWindowClickListener {
private static final long UPDATE_INTERVAL = 5000;
private static final long FASTEST_INTERVAL = 1000;
private GoogleMap map = null;
private LocationClient locationClient;
private Location myLocation;
private LocationRequest locationRequest;
private LatLng newPosition;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
locationClient = new LocationClient(this, this, this);
locationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setInterval(UPDATE_INTERVAL)
.setFastestInterval(FASTEST_INTERVAL).setSmallestDisplacement(10);
if (locationClient != null)
// connect the client to the Google Play services
locationClient.connect();
}
#Override
public void onLocationChanged(Location location) {
newPosition = new LatLng(location.getLatitude(), location.getLongitude());
myLocation = location;
if (myLocation != null)
button.setClickable(true);
else
button.setClickable(false);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(newPosition, 20));
}
#Override
public void onConnectionFailed(ConnectionResult arg0) {
}
#Override
public void onConnected(Bundle arg0) {
Toast.makeText(this, "I'm bringing you to your area.", Toast.LENGTH_SHORT).show();
// start periodic updates
locationClient.requestLocationUpdates(locationRequest, this);
}
#Override
public void onDisconnected() {
Toast.makeText(this, "Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
}
public ProgressDialog getProgress() {
return progress;
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onRestart() {
super.onRestart();
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onStop() {
super.onStop();
}
#Override
protected void onDestroy() {
if (locationClient.isConnected() && locationClient != null) {
locationClient.removeLocationUpdates(this);
locationClient.disconnect();
}
super.onDestroy();
}
}
my Service Class:
public class locationBackgroundService extends Service implements LocationListener, GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
private LocationRequest mLocationRequest;
private LocationClient mLocationClient;
public locationBackgroundService() {
}
#Override
public void onCreate() {
mLocationRequest = LocationRequest.create();
// mLocationRequest.setInterval(CommonUtils.UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// mLocationRequest.setFastestInterval(CommonUtils.FAST_INTERVAL_CEILING_IN_MILLISECONDS);
mLocationClient = new LocationClient(getApplicationContext(), this, this);
mLocationClient.connect();
}
#Override
public void onStart(Intent intent, int startId) {
int start = Service.START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
#Override
public void onConnected(Bundle arg0) {
Log.i("info", "Location Client is Connected");
mLocationClient.requestLocationUpdates(mLocationRequest, this);
Log.i("info", "Service Connect status :: " + isServicesConnected());
}
#Override
public void onDisconnected() {
Log.i("info", "Location Client is Disconnected");
}
#Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}
private boolean isServicesConnected() {
// Check that Google Play services is available
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(locationBackgroundService.this);
// If Google Play services is available
if (ConnectionResult.SUCCESS == resultCode) {
return true;
} else {
return false;
}
}
#Override
public void onDestroy() {
mLocationClient.removeLocationUpdates(this);
super.onDestroy();
}
}
is it possible to do that?
thanks in advance.
example of LocationService.class
package com.example.myapp;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.example.myapp.activities.MainActivity;
public class LocationService extends Service implements
LocationListener,
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
public static final String LOCATION_SERVICE = "LocationService";
private LocationClient mLocationClient;
private LocationRequest mLocationRequest;
float minDist = 100f;
int minTime = 10000;
PendingIntent contentIntent;
public static String USER_LATITUDE = "user_latitude";
public static String USER_LONGITUDE = "user_longitude";
#Override
public void onCreate() {
Log.d("onCreate", "");
super.onCreate();
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
mLocationClient = new LocationClient(this, this, this);
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(minTime);
//mLocationRequest.setSmallestDisplacement(minDist);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
mLocationClient.connect();
return Service.START_NOT_STICKY;
}
#Override
public void onLocationChanged(Location location) {
float latitude = (float) location.getLatitude();
float longitude = (float) location.getLongitude();
Log.d("onLocationChanged. latLng", latitude + ", " + longitude);
Intent intent = new Intent(LOCATION_SERVICE);
intent.putExtra(USER_LATITUDE, latitude);
intent.putExtra(USER_LONGITUDE, longitude);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
#Override
public void onDestroy() {
super.onDestroy();
if (mLocationClient.isConnected() ) {
stopPeriodicUpdates();
}
}
#Override
public void onConnected(Bundle bundle) {
Log.d(LOCATION_SERVICE, "onConnected");
startPeriodicUpdates();
}
#Override
public void onDisconnected() {
Log.d(LOCATION_SERVICE, "onDisconnected");
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(LOCATION_SERVICE, "onConnectionFailed");
}
public void startPeriodicUpdates() {
mLocationClient.requestLocationUpdates(mLocationRequest, this);
Log.d(LOCATION_SERVICE, "startPeriodicUpdates");
}
private void stopPeriodicUpdates() {
mLocationClient.removeLocationUpdates(this);
Log.d(LOCATION_SERVICE, "stopPeriodicUpdates");
}
}
add in MainActivity.class
private boolean servicesConnected() {
int resultCode =
GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == resultCode) {
Log.d("servicesConnected", "Google play services isConnected");
return true;
} else {
return false;
}
}
private BroadcastReceiver mLocationReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
float latitude = intent.getFloatExtra(LocationService.USER_LATITUDE, 0);
float longitude = intent.getFloatExtra(LocationService.USER_LONGITUDE, 0);
//do something
}
};
#Override
public void onPause() {
super.onPause();
Intent service = new Intent(this, LocationService.class);
IntentFilter intentLocationServiceFilter = new IntentFilter(LocationService
.LOCATION_SERVICE);
LocalBroadcastManager.getInstance(this)
.registerReceiver(mLocationReceiver, intentLocationServiceFilter);
if (servicesConnected()) {
startService(service);
}
}
And do not forget add to AndroidManifest file your LocationService

Listening to bound location service from an activity

I've got a location service to have an updated location in my app. I bind it to every activity requiring location data, and now I'd like to know in these activities when the location listener in the service receives events such as onLocationChanged, onProviderEnabled... How can I do that?
In my activities
private ServiceConnection mConnection;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Bind location service
bindService(new Intent(this, LocationService.class), mConnection, Context.BIND_AUTO_CREATE);
// Activity stuff...
}
#Override
protected void onDestroy() {
super.onDestroy();
// Unbind LocationService
context.unbindService(mConnection);
}
LocationService.java
public class LocationService extends Service implements LocationListener {
LocationManager locationManager;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
// Update after minimum 5 minutes and if user has moved at least 100 meters.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 60 * 1000, 100, this);
Location loc = getBestLocation(locationManager);
if(loc!=null){
GlobalVars.lat = (Double) (loc.getLatitude());
GlobalVars.lng = (Double) (loc.getLongitude());
}
}
}
public void onLocationChanged(Location loc) {
GlobalVars.lat = (Double) (loc.getLatitude());
GlobalVars.lng = (Double) (loc.getLongitude());
}
public static Location getBestLocation(LocationManager locationManager) {
Location location_gps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location_network = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// If both are available, get the most recent
if(location_gps!=null && location_network !=null) {
return (location_gps.getTime() > location_network.getTime())?location_gps:location_network;
}
else if(location_gps==null && location_network ==null){
return null;
}
else
return (location_gps==null)?location_network:location_gps;
}
public void onProviderEnabled(String s){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 60 * 1000, 0, this);
}
public void onProviderDisabled(String s){
locationManager.removeUpdates(this);
GlobalVars.lat = null;
GlobalVars.lng = null;
}
public void onStatusChanged(String s, int i, Bundle b){}
#Override
public void onDestroy() {
locationManager.removeUpdates(this);
}
}
I would do it on this way:
Create LocationListener class..
Each time i get a new location, send broadcast message with specific predefined ACTION and in extras put lat and lon.
Each activity creates broadcast listener for this message with intentFilter(Action);
Get extras from the intent.

Best error handling approach for GPS lookup in Android?

I'm beta testing my first Android app and have had a few users mention that when they attempt to lookup by GPS it hangs. In order to improve error handling around this I wanted to get the opinion of people who have apps in the wild.
My current activity does the following to kick off the lookup
findViewById(R.id.gpsButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
MyLocationListener mlocListener = new MyLocationListener();
Criteria locationCriteria = new Criteria();
locationCriteria.setAccuracy(Criteria.ACCURACY_FINE);
mlocManager.requestLocationUpdates(mlocManager.getBestProvider(locationCriteria, true), 0, 0, mlocListener);
}
});
The implementation of my custom location lookup class is below
public class MyLocationListener implements LocationListener {
private boolean alreadyLocatedDevice;
private ProgressDialog dialog;
public MyLocationListener() {
this.dialog = ProgressDialog.show(LocationLookup.this, "", "Loading...");
}
#Override
public void onProviderDisabled(String provider) {
this.dialog.dismiss();
DialogHelper.showDialogWithMessageAndTitle("", "You don't currently have location services turned on", LocationLookup.this);
}
#Override
public void onLocationChanged(android.location.Location location) {
if (!alreadyLocatedDevice) {
alreadyLocatedDevice = true;
Location loc = new Location();
loc.setLng(Double.toString(location.getLongitude()));
loc.setLat(Double.toString(location.getLatitude()));
((AppDelegate) getApplicationContext()).setSelectedLocation(loc);
Intent findKioskLocation = new Intent(LocationLookup.this, FindKioskLocation.class);
this.dialog.dismiss();
startActivity(findKioskLocation);
}
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
//To change body of implemented methods use File | Settings | File Templates.
}
#Override
public void onProviderEnabled(String s) {
//To change body of implemented methods use File | Settings | File Templates.
}
}
And finally I've added both the ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions in my manifest file.
Any help would be much appreciated!
Here is a nice implementation that I recnetly looked at. Basically by default it uses GPS to get a location. If no location can be found within a certain time period or no satellites are available it switches to Network.
Hope this helps
A Stacktrace from the logcat would have helped you and other developers here understand where the problem is coming from. Try to ask the users to recreate the problem if possible and find out when it is occurring.
As for guidance with the locaton manager, Google Developers just posted a blog recently and also updated the docs on how to use location manager. Check the documentation here and also the blog post which explains it with an example. That might help you better. The blog post also explains how to use different location providers and how to be user friendly and guidance necessary in most ases when using location in Android applications.
For anyone who might follow this thread -I found a mixture of my own approach (admittedly hackish in this code example) and the one mentioned by #bear to work without any issues (plus the location lookup was fast/accurate and error free)
I found the example listed by #bear to be a little more complex than I needed. For starters I wanted to kick off the GPS lookup when a button was clicked and have a simple async task wrapping this so it would throw up a dialog/etc
Next I wanted the exact latitude and longitude (no need to pass this off to another class because my example was simply to use the lat + lng to locate a resource and plot it)
So if you can follow my untested rather copy/paste approach here goes...
Inside your activity you would spin up the service during an onclick lets say ...
LocationManager networkManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationManager gpsManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationService locationProcessor = new LocationService(YourActivityName.this, networkManager, gpsManager, dialog);
locationProcessor.onStartCommand();
Now the location service itself
package com.epicsoftware.android.global;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import com.epicsoftware.android.activity.LocationLookup;
public class LocationService {
private LocationManager networkLm;
private LocationManager gpsLm;
private LocationListener networkListener;
private LocationListener gpsListener;
private boolean isRunning;
private boolean networkLocDisabled;
private boolean gpsLocDisabled;
private Context activity;
private LocationManager tmpNetworkManager;
private LocationManager tmpGpsManager;
private Handler locationHandler;
private ProgressDialog dialog;
private boolean gpsUpdated;
private boolean done;
public LocationService(final Context activity, LocationManager networkManager, LocationManager gpsManager, ProgressDialog dialog) {
this.tmpNetworkManager = networkManager;
this.tmpGpsManager = gpsManager;
this.activity = activity;
this.dialog = dialog;
}
public void onStartCommand() {
if (!isRunning) {
isRunning = true;
startLocationListeners();
locationHandler = new Handler();
getLocationByZip.start();
}
}
private void startLocationListeners() {
networkListener = new NetworkLocationListener();
gpsListener = new GpsLocationListener();
networkLm = tmpNetworkManager;
gpsLm = tmpGpsManager;
networkLm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkListener);
gpsLm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsListener);
}
private Thread getLocationByZip = new Thread() {
public void run() {
try {
for (int i = 0; i < 15;) {
if (!locationDisabled() || !gpsUpdated) {
try {
Thread.sleep(1000);
} catch (Exception e) {
break;
}
i++;
} else {
break;
}
}
locationHandler.post(monitorTheNetworkAndGpsProviders);
} catch (Exception e) {
killService();
done = true;
}
}
};
private Runnable monitorTheNetworkAndGpsProviders = new Runnable() {
#Override
public void run() {
killService();
dialog.dismiss();
if (!done) {
done = true;
((LocationLookup) activity).warnUserThatLocationServicesAreDisabledOrFailed();
}
}
};
private boolean locationDisabled() {
if (gpsLocDisabled && networkLocDisabled) {
done = true;
((LocationLookup) activity).warnUserThatLocationServicesAreDisabledOrFailed();
return true;
} else {
return false;
}
}
private void updateDb(Double lat, Double lon) {
done = true;
((LocationLookup) activity).setLocationDataAndSpinUpNextActivity(lat, lon);
}
public void killService() {
networkLm.removeUpdates(networkListener);
gpsLm.removeUpdates(gpsListener);
}
public class NetworkLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
if (location != null) {
updateDb(location.getLatitude(), location.getLongitude());
}
}
#Override
public void onProviderDisabled(String provider) {
networkLocDisabled = true;
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
public class GpsLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
if (location != null) {
gpsUpdated = true;
updateDb(location.getLatitude(), location.getLongitude());
}
}
#Override
public void onProviderDisabled(String provider) {
gpsLocDisabled = true;
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}

Categories

Resources