FusedLocationService not aquiring GPS signal in Android - android

I'm using the new FusedLocationService, but, despite I'm getting latitude and longitude while being indoors, I don't see GPS signal being aquired (small gps point in the notifications bar not appearing).
I'm using the example from here adapted to location service
What I don't understand is why GPS signal is not being searched for despite GPS is enabled (but I'm getting coordinates, I guess I getting those from wifi or cell id)
In my Application class I create a Service (this is a ServicesManager, which in turn creates another service (to retrieve locations). I'm sending as context to LocationClient the ServicesManager as it is a context (because it's a Service).
Thanks in advance. Guillermo.
UPDATE
If I turn off the Use Wireless Network option in location services on the phone while GPS is enabled I don't get locations at all. So something is happening with the FusedLocationService and the GPS.
I'll add code so it can be better understood
In Application class I'm using the LocalService example from: here
private ServicesManager mBoundService;
private boolean mIsBound;
private ServiceConnection mConnectionToServicesManager = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((ServicesManager.LocalBinder)service).getService();
servicesManager = mBoundService;
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
void doBindServiceManagerService() {
bindService(new Intent(this, ServicesManager.class), mConnectionToServicesManager, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
void doUnbindService() {
if (mIsBound) {
unbindService(mConnectionToServicesManager);
mIsBound = false;
}
}
Then ServicesManager extends Service and in the constructor I'm writing this:
fusedLocationService = new FusedLocationService(this);
Then I call to:
fusedLocationService.startListeningLocationUpdates(this);
This is the implementation of startListeningLocationUpdates in FusedLocationService class
public boolean startListeningLocationUpdates(Context context) {
if (!GdpTesisApplication.IsGooglePlayServicesAvailable) {
return false;
}
mDetectionRequester.requestUpdates();
FusedLocationService.IsServiceRunning = true;
return true;
}
And requestUpdates() tries to connecto to GooglePlayServices
private void requestConnection() {
getFusedLocationClient().connect();
}
#Override
public void onConnected(Bundle arg0) {
continueRequestLocationUpdates();
}
private void continueRequestLocationUpdates() {
locationrequest = LocationRequest.create();
locationrequest.setInterval(LocationUtils.DETECTION_INTERVAL_MILLISECONDS);
getFusedLocationClient().requestLocationUpdates(locationrequest, createRequestPendingIntent());
}
private PendingIntent createRequestPendingIntent() {
if (null != getRequestPendingIntent()) {
return mFusedLocationPendingIntent;
} else {
Intent intent = new Intent(mContext, FusedLocationIntentService.class);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
setRequestPendingIntent(pendingIntent);
return pendingIntent;
}
}
Finally I have an IntentService which onHandleIntent extracts the location and shows it with:
Location location = intent.getParcelableExtra(LocationClient.KEY_LOCATION_CHANGED);
I don't know why GPS is not working. Any idea?

Take a look here:
http://developer.android.com/reference/com/google/android/gms/location/LocationRequest.html#setPriority%28int%29
You didn't set a priority for your LocationRequest. Try with PRIORITY_HIGH_ACCURACY.

Related

How to fix Service Instance Issue?

I am starting a service based on Alarm Manager in every 20 seconds which sends GPS data to my server.
The problem is my heap and allocated heap size goes on increasing. When I analysed the heap dump, I found the number of service instances is equal to the number of calls to startService(). How to avoid this issue?
public class SystemBootListener extends BroadcastReceiver {
// Restart service every 30 seconds
private static final long REPEAT_TIME = 1000 * 10;
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, StartLocationServiceAfterReboot.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, PendingIntent.FLAG_UPDATE_CURRENT);
// Start 20 seconds after boot completed - so that all providers are initialized by then
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 20);
// Trigger every 10 seconds
// InexactRepeating allows Android to optimize the energy consumption
AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME, pending);
}
}
public class StartLocationServiceAfterReboot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(AppSettings.isRouteConfigured(context)){
AppSettings.setServiceRunning(context, Boolean.TRUE);
Intent service = new Intent(context, GPSComputationService.class);
context.startService(service);
}
}
}
public class GPSComputationService extends Service {
private static final int MAX_TIME_TO_FETCH_NEW_LOCATION = 8000;
private final IBinder mBinder = new ServiceBinder();
private Timer timerToFetchLocInfoFromProviders = null;
private LocationManager locationManager = null;
private boolean gpsProviderEnabled=false;
private boolean networkProviderEnabled=false;
private int numberOfSatellites = 0;
private GPSData bestKnownLocation = new GPSData();
private TCPWriter tcpWriter ;
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
tcpWriter= new TCPWriter(this);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
/*tcpWriter= new TCPWriter(this);*/
computeBestLocation();
return Service.START_STICKY;
}
private void stopGPSComputationService(){
stopSelf();
}
#Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
public class ServiceBinder extends Binder {
public GPSComputationService getService() {
return GPSComputationService.this;
}
}
public GPSData getBestKnownLocation() {
return bestKnownLocation;
}
public void publishBestKnownLocation(GPSData bestKnownLocation) {
this.bestKnownLocation = bestKnownLocation;
sendBestKnownLocationToNMEAServer();
}
public void sendBestKnownLocationToNMEAServer(){
if(getBestKnownLocation() == null){
stopGPSComputationService();
return;
}
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.getDeviceId();
NMEAData dataPacketToWrite = new NMEAData(
telephonyManager.getDeviceId(),
getBestKnownLocation().getLatitude(),
getBestKnownLocation().getLongitude(),
getBestKnownLocation().getTimeStamp(),
getBestKnownLocation().getSpeed(),
getBestKnownLocation().getNumberOfSatellites()
);
tcpWriter.sendMessage(NMEAServerTypes.MVT600,
dataPacketToWrite);
stopGPSComputationService();
}
public GPSData computeBestLocation() {
Log.d("#############GPSComputation Status", "Running.......");
try{
if(locationManager==null)
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Add status listener for satellite count
locationManager.addGpsStatusListener(gpsStatusListener);
Criteria criteria = new Criteria();
criteria.setSpeedRequired(true);
criteria.setBearingRequired(true);
List<String> providers = locationManager.getProviders(criteria, false);
//Capture if the GPS/Network providers have been disabled.
try{
gpsProviderEnabled=providers.contains(LocationManager.GPS_PROVIDER) &&
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}catch(Exception e){
}
try{
networkProviderEnabled=providers.contains(LocationManager.NETWORK_PROVIDER) &&
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}catch(Exception e){
}
if(!gpsProviderEnabled && !networkProviderEnabled)
return null;
if(gpsProviderEnabled)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
if(networkProviderEnabled)
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
timerToFetchLocInfoFromProviders=new Timer();
timerToFetchLocInfoFromProviders.schedule(new GetLastKnownGoodLocation(), MAX_TIME_TO_FETCH_NEW_LOCATION);
locationManager.removeGpsStatusListener(gpsStatusListener);
//Finally store the data in backend Service
return getBestKnownLocation() ;
}catch(Exception e){
return null;
}
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timerToFetchLocInfoFromProviders.cancel();
publishBestKnownLocation(extractAllGeoInfFromLocation(location));
locationManager.removeUpdates(this);
locationManager.removeUpdates(locationListenerNetwork);
locationManager.removeGpsStatusListener(gpsStatusListener);
gpsStatusListener = null;
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
//listen for gps status changes to capture number of satellites.
GpsStatus.Listener gpsStatusListener = new GpsStatus.Listener() {
#Override
public void onGpsStatusChanged(int event) {
if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS || event == GpsStatus.GPS_EVENT_FIRST_FIX) {
GpsStatus status = locationManager.getGpsStatus(null);
Iterable<GpsSatellite> sats = status.getSatellites();
// Check number of satellites in list to determine fix state
int tempNumberOfSatellites = 0;
for (GpsSatellite sat : sats) {
if(sat.usedInFix())
tempNumberOfSatellites++;
}
numberOfSatellites = tempNumberOfSatellites;
}
}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timerToFetchLocInfoFromProviders.cancel();
publishBestKnownLocation(extractAllGeoInfFromLocation(location));
locationManager.removeUpdates(this);
locationManager.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
class GetLastKnownGoodLocation extends TimerTask {
#Override
public void run() {
locationManager.removeUpdates(locationListenerGps);
locationManager.removeUpdates(locationListenerNetwork);
Location bestKnownNetworkLocation = null, bestKnownGPSLocation=null;
if(gpsProviderEnabled)
bestKnownGPSLocation=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(networkProviderEnabled)
bestKnownNetworkLocation=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(bestKnownGPSLocation!=null && bestKnownNetworkLocation!=null){
if(bestKnownGPSLocation.getTime()>bestKnownNetworkLocation.getTime())
publishBestKnownLocation(extractAllGeoInfFromLocation(bestKnownGPSLocation));
else
publishBestKnownLocation(extractAllGeoInfFromLocation(bestKnownNetworkLocation));
return;
}
if(bestKnownGPSLocation!=null){
publishBestKnownLocation(extractAllGeoInfFromLocation(bestKnownGPSLocation));
return;
}
if(bestKnownNetworkLocation!=null){
publishBestKnownLocation(extractAllGeoInfFromLocation(bestKnownNetworkLocation));
return;
}
AppLog.logWarningMsg("Bad luck-NO BEST LOCATION AVAILABLE");
publishBestKnownLocation(null);
}
}
private GPSData extractAllGeoInfFromLocation(Location location){
bestKnownLocation = new GPSData();
bestKnownLocation.setLatitude(location.getLatitude());
bestKnownLocation.setLongitude(location.getLongitude());
bestKnownLocation.setTimeStamp(location.getTime());
bestKnownLocation.setSpeed(location.getSpeed()*3.8);
bestKnownLocation.setNumberOfSatellites(numberOfSatellites);
return bestKnownLocation;
}
}
There is only one instance of service. As per the document
Multiple calls to Context.startService() do result in multiple corresponding calls to onStartCommand()),
But only one service instance can exist.
On startService(), the Android system calls the service's onStartCommand() method. If the service is not already running, the system first calls onCreate(), then calls onStartCommand().
The only thing that can create this kind of scenario is that you have some kind of memory leak.
Your service did it's work and stopped but didn't garbage collected. it probably happen few times and that is why you see many instances of it.
It's hard to find memory leaks but i would suggest you to start from the listeners. check if you unregistered them at the right time.
This link can help you to detect the leak:
https://developer.android.com/studio/profile/am-memory.html
Some tips to improve and simplify your code:
You want to use one shot service to report GPS coordinates. For this purpose IntentService is much better and it runs in a background thread by design.
https://developer.android.com/training/run-background-service/create-service
Use PendingIntent.getService() instead of call StartLocationServiceAfterReboot which just launch other Android component. You can do that immediately. You save one step.
https://developer.android.com/reference/android/app/PendingIntent.html#getService(android.content.Context,%2520int,%2520android.content.Intent,%2520int)
Every time you use a resource (like GPS, Sensor, etc...) you have to also write the release part. As I see, you register listeners to GPS Service but never release (unregister) them.
What does AppSettings.setServiceRunning(context, Boolean.TRUE);? My guess you save this into SharedPreference. This can be compromised when app force-stopped or device restarted or suddenly shut-down. Maybe better way this https://stackoverflow.com/a/5921190/5823014
Avoid to use static on Context, Activity, Service, BroadcastReceiver, Application instance. I'm not see in your code snippet, just a general advice to prevent memory-leaks.

Unable to display notification when entering GeoFence in Android

I am working on GeoFencing in Android and I am stuck at one point. My task is to show notification to user when he enters/exits a Geofence area defined by me.
Here is my code:
Activity class
public class TestMapActivity extends FragmentActivity implements
OnMarkerDragListener,ConnectionCallbacks, OnConnectionFailedListener,OnAddGeofencesResultListener {
private static GoogleMap map;
private LocationClient mLocationClient;
private PendingIntent mGeofencePendingIntent;
private SimpleGeoFence fence;
private List<Geofence> mGeoList;
private LocationRequest localRequest;
private GeofenceReceiver mBroadcastReceiver;
private IntentFilter mIntentFilter;
#Override
protected void onCreate(Bundle saveInstance)
{
super.onCreate(saveInstance);
setContentView(R.layout.activity_map);
map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setOnMarkerDragListener(this);
CameraPosition INIT =
new CameraPosition.Builder()
.target(new LatLng(19.0222, 72.8666))
.zoom(17.5F)
.bearing(300F) // orientation
.tilt( 50F) // viewing angle
.build();
map.moveCamera( CameraUpdateFactory.newCameraPosition(INIT) );
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.add_fence:
Toast.makeText(TestMapActivity.this, "Add fence is Selected", Toast.LENGTH_LONG).show();
fence= new SimpleGeoFence();
fence.toGeofence();
addMarkerForFence(fence);
addIntentForFence();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void addMarkerForFence(SimpleGeoFence fence){
if(fence == null){
// display an error message and return
return;
}
//Instantiates a new CircleOptions object + center/radius
CircleOptions circleOptions = new CircleOptions()
.center( new LatLng(19.0216, 72.8646 ))
.radius( 500 )
.fillColor(0x40ff0000)
.strokeColor(Color.TRANSPARENT)
.strokeWidth(2);
map.addCircle(circleOptions);
map.addMarker( new MarkerOptions()
.position( new LatLng(19.0216, 72.8646) )
.title("Fence " +fence.getId())
.snippet("Radius: " +fence.getRadius()) ).showInfoWindow();
// Get back the mutable Circle
Circle circle = map.addCircle(circleOptions);
}
public void addIntentForFence()
{
Geofence geoFence= fence.toGeofence();
mGeoList = new ArrayList<Geofence>();
mGeoList.add(geoFence);
mLocationClient = new LocationClient(this, this, this);
mLocationClient.connect();
}
#Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
mGeofencePendingIntent = createRequestPendingIntent();
localRequest = LocationRequest.create();
localRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
localRequest.setInterval(5000);
mLocationClient.addGeofences(mGeoList, mGeofencePendingIntent , this);
}
private PendingIntent createRequestPendingIntent() {
if (null != mGeofencePendingIntent) {
return mGeofencePendingIntent;
} else {
Intent intent = new Intent("com.example.ACTION_RECEIVE_GEOFENCE");
System.out.println("Intent" +intent);
//sendBroadcast(intent);
return PendingIntent.getBroadcast(
getApplicationContext(),
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
}
} //end oncreate
GeofenceReceiver class
public class GeofenceReceiver extends BroadcastReceiver
{
public Context context;
Intent broadcastIntent = new Intent();
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
this.context = context;
broadcastIntent.addCategory("com.example.CATEGORY_LOCATION_SERVICES");
String action= intent.getAction();
if (LocationClient.hasError(intent)) {
//do something
}
else
{
handleEnterExit(intent);
}
}
private void handleEnterExit(Intent intent) {
int transition = LocationClient.getGeofenceTransition(intent);
System.out.println("transition" +transition); //getting -1 over here
if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER)
|| (transition == Geofence.GEOFENCE_TRANSITION_EXIT)) {
//send notification over here
}
}
AndroidManifest.xml container Receiver
<receiver android:name="com.example.GeofenceReceiver" android:exported="false">
<intent-filter >
<action android:name="com.example.ACTION_RECEIVE_GEOFENCE"/>
</intent-filter>
</receiver>
So basically I am able to see my GeoFence getting created but i don't get notification for the same.
does anyone have a solution for this?
Just check it sample code. u r taking Geo-fencing but you are not mention location client and location connect so that google play service not connect to client and you are not able to receive notification take google requester file and check main acitvity where geofence add in list and also connect location client hope so its work for you
So from the comments you are saying that you are able to add a geofence but you are not getting a notification. Firstly it looks as if you have used the android's geofence sample code which is their on their website and you have changed it so it uses a receiver instead of a service. For me i did the same and this might not seem like the right solution my answer is :
1.) Follow the same steps/code in the google'e geofence : http://developer.android.com/training/location/geofencing.html
2.) since you are changing the service to a receiver , in your createRequestPendingIntent() method are you returning the right broadcast intent ? I mean you have this line but is it reaching there ? is something getting returned there ?
return PendingIntent.getBroadcast(
mActivity,0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
3.)Do you have a LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, mIntentFilter); in your onResume() ?
I would follow the google's sample project again and just make sure you are changing the service to receiver part right and you should be fine.
I had the same problem.
I think that's because you are using the application context for your pending intent, try to use the activity instead.

service instance still null after binding

I have a service, and I am trying to bind an activity to it. The problem is...after running bindService(..), the service instance that Im setting inside the serviceconnection is still null, and I dont know why.
private ConnectionService conn;
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
conn = ((ConnectionService.ConnectionBinder)service).getService();
Toast.makeText(main_tab_page.this, "Connected", Toast.LENGTH_SHORT)
.show();
}
#Override
public void onServiceDisconnected(ComponentName name) {
conn = null;
}
};
#Override
protected void onStart()
{
super.onStart();
//check start connection service
if(conn == null)
{
Intent serviceIntent = new Intent(this, ConnectionService.class);
bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
}
//connect to server
server.conn = conn;
//THIS STATEMENT FAILS: NULL REFERENCE, conn is Null here, and I have no idea why
conn.ConnectToServer(server);
server.StartReader();
}
Yes: The service is defined in the manifest.
Yes: I can start the service from the MAIN Activity (this code resides in an activity that is started BY the main activity, which is where i need to bind to the service) I have checked to make sure the service actually does start....it does
According to every example i've managed to locate for bound services, this should be working. Can anyone tell me why its not?
Edit: Add service code definition
public class ConnectionService extends Service{
private BlockingQueue<String> MessageQueue;
public final IBinder myBind = new ConnectionBinder();
public class ConnectionBinder extends Binder {
ConnectionService getService() {
return ConnectionService.this;
}
}
private Socket socket;
private BufferedWriter writer;
private BufferedReader reader;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(MessageQueue == null)
MessageQueue = new LinkedBlockingQueue<String>();
return Service.START_NOT_STICKY;
}
#Override
public IBinder onBind(Intent arg0) {
return myBind;
}
//some other code that has everything to do with what the service does, and nothing to do with how it should be started/run
}
Please check the service is declared in Manifest.

How can I pass ApplicationContext into the function in a new Thread()?

I need to get location information about GPS location every 30 seconds in an infinity loop and send in to a server via HTTP-request. Endless cycle if GPS scanning should be stopped If i get an appropriate response from the server. Service is called DataTransferService, gps scanner is called GPSTracker for this and service . The problem is that I can't get a proper Context for my GPSTracker in my new Thread(new Runnable()).
If i create a ThreadHandler my MainActivity will freeze. In addition, context is also null even if I'm initializing in my service to use later.
Here is my DataTransferService.java
public class DataTransferService extends Service {
final static String LOG_TAG = "---===> service";
private boolean isRunning = false;
private GPSTracker gps;
private double lat;
private double lng;
public void onCreate() {
super.onCreate();
Log.d(LOG_TAG, "onCreate");
}
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(LOG_TAG, "onStartCommand");
if (!isRunning) {
StartLocationService();
isRunning = true;
}
return super.onStartCommand(intent, flags, startId);
}
public void onDestroy() {
isRunning = false;
super.onDestroy();
Log.d(LOG_TAG, "onDestroy");
}
public IBinder onBind(Intent intent) {
Log.d(LOG_TAG, "onBind");
return null;
}
private void StartLocationService(final String login, final String password) {
Thread thread = new Thread(new Runnable() {
public void run() {
Log.d(LOG_TAG, "StartLocationService() started");
while (true) {
//CHECK IF SERVICE IS RUNNING
if (!isRunning) {
stopSelf();
break;
}
//HERE IS THE PROBLEM <----------------
gps = new GPSTracker(getApplicationContext());
//GETTING GPS INFO
if(gps.canGetLocation()){
lat = gps.getLatitude();
lng = gps.getLongitude();
}else{
gps.showSettingsAlert();
}
try {
Log.d(LOG_TAG, String.format("location is: %f; %f", lat, lng));
//i wanted to send HTTP request to the server here with the gps coordinates
} catch (MalformedURLException e) {
e.printStackTrace();
}
//SERVICE DELAY
try {
TimeUnit.SECONDS.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
}
As I wanted to stop the endless loop when user presses "Stop" button I've created bool variable which indicated if the cycle should bee countinued or stopped.
UPDATE:
I added some debug outputs(before my Thread() and inside It) to determine if getApplicationContext() result isreally different and I found out, that all the objects are equal.
I used Log.d(LOG_TAG, getApplicationContext().toString()); before the Thread() and Log.d(LOG_TAG, mApplication.getInstance().getApplicationContext().toString()); inside the Thread(), where mApplication - is my singleton.
Results:
D/---===> service(7264): com.my.program.MyApplication#40ce3ee0
D/---===> service(7264): StartLocationService() started
D/---===> service(7264): com.my.program.MyApplication#40ce3ee0
Here is my GPSTracker.java if you are interested in it: http://pastebin.com/p6e3PGzD
Have you checked why your Activity will be frozen if you use HandlerThread? You can also create a Handler out of your thread, and send data via handler within your thread.
How can I pass ApplicationContext into the function in a new Thread()
You don't need to pass application context as you can access it from anywhere by
Defining android Application class in your codebase.
Just google "define application class android example"
http://developer.android.com/reference/android/app/Application.html
http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/
MyApplication.instance.getApplicationContext();
should be what you want. (where instance will be the singleton object you will define)

Testing Mock Locations not Working

I have written a simple Gps Service. Now I am writing a testcase for testing it. I am trying to send a mock location but the onLocation changed is not getting called in my GpsService location listener.
Here is my GpsService
/**
* returns the binder object that client of this can bind to
*/
#Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
#Override
public void onCreate() {
super.onCreate();
Log.e("GpsService","StartService");
}
private class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
Log.e("GpsService","Location in GpsService"+loc.toString());
if(loc.hasAccuracy() && loc.getAccuracy() <= minAccuracyMeters)
{
if(targetLocation != null)
{
float distance = loc.distanceTo(targetLocation);
Log.e("GpsService ","Location latitude +"+loc.getLatitude()+"longitude "+loc.getLongitude());
if(distance < 5.0)
Toast.makeText(getBaseContext(), "You have reached the target location", Toast.LENGTH_LONG).show();
}
}
}
public void setTargetLocation (Location _location){
targetLocation = _location;
}
/**
* Class for clients to access. Because we know this service always runs in
* the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
GpsService getService() {
return GpsService.this;
}
}
private final IBinder mBinder = new LocalBinder();
#Override
public void onDestroy() {
if(lm != null)
lm.removeUpdates(locationListener);
super.onDestroy();
}
//Setting up for test case
public void setUpProvider(String provider){
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(provider,
minTimeMillis,
minDistanceMeters,
locationListener);
}
Now here is my TestCase
public class MockLocationTest extends ServiceTestCase {
private LocationManager locationManager = null;
private static final String MOCK_GPS_PROVIDER = "MOCK_GPS_PROVIDER";
private List<String> data = new ArrayList<String>();
public MockLocationTest(Class<GpsService> serviceClass) {
super(serviceClass);
}
public MockLocationTest() {
super(GpsService.class);
}
#Override
protected void setupService() {
super.setupService();
System.out.println("Service Set Up");
Intent intent = new Intent(getContext(),GpsService.class);
startService(intent);
assertNotNull("The Service should not be null",getService());
System.out.println("Service Started");
}
#Override
protected void setUp() throws Exception {
super.setUp();
}
#MediumTest
public void testBindable() {
Intent startIntent = new Intent();
startIntent.setClass(getContext(), GpsService.class);
IBinder service = bindService(startIntent);
assertNotNull("Bound to service ",service);
getService().setUpProvider(MOCK_GPS_PROVIDER);
Location demo = new Location(MOCK_GPS_PROVIDER);
demo.setLatitude(22.579937);
demo.setLongitude(88.486805);
getService().setTargetLocation(demo);
System.out.println("Test Bindable");
}
#SmallTest
public void testMockData(){
locationManager = (LocationManager) getContext().getSystemService((Context.LOCATION_SERVICE));
locationManager.setTestProviderEnabled(MOCK_GPS_PROVIDER, true);
Location location = new Location(MOCK_GPS_PROVIDER);
location.setLatitude(22.579937);
location.setLongitude(88.486805);
location.setTime(System.currentTimeMillis());
// show debug message in log
// provide the new location
locationManager.setTestProviderLocation(MOCK_GPS_PROVIDER, location);//send mock data
}
#Override
protected void tearDown() throws Exception {
super.tearDown();
}
}
Unfortunately I cant see any log in the onLocationChange callback in the service. The testcase runs successfully.
I have added the following in both my service and testservice's manifest
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
Can anybody help me here
I also tried putting this in one function
#LargeTest
public void testBindable() {
Intent startIntent = new Intent();
startIntent.setClass(getContext(), GpsService.class);
IBinder service = bindService(startIntent);
assertNotNull("Bound to service ",service);
LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
locationManager.addTestProvider(MOCK_GPS_PROVIDER, false, false,
false, false, false, false, false, 0, 5);
locationManager.setTestProviderEnabled(MOCK_GPS_PROVIDER, true);
getService().setUpProvider(MOCK_GPS_PROVIDER,locationManager);
Location demo = new Location(MOCK_GPS_PROVIDER);
demo.setLatitude(22.579937);
demo.setLongitude(88.486805);
demo.setTime(System.currentTimeMillis());
getService().setTargetLocation(demo);
Location narkeldanga = new Location(MOCK_GPS_PROVIDER);
narkeldanga.setLatitude(22.578986);
narkeldanga.setLongitude(88.470154);
narkeldanga.setTime(System.currentTimeMillis());
locationManager.setTestProviderLocation(MOCK_GPS_PROVIDER, narkeldanga);
try {
Thread.sleep(10000);
// gracefully handle Thread interruption (important!)
if(Thread.currentThread().isInterrupted())
throw new InterruptedException("");
} catch (InterruptedException e) {
}
locationManager.setTestProviderLocation(MOCK_GPS_PROVIDER, demo);
try {
Thread.sleep(10000);
// gracefully handle Thread interruption (important!)
if(Thread.currentThread().isInterrupted())
throw new InterruptedException("");
} catch (InterruptedException e) {
}
System.out.println("Test Bindable");
}
but to no avail.
This is a problem I and others have encountered in jUnit tests. Namely, testBindable() will run in one thread while your service is running in another. The result is that the location service is still setting up and hasn't even thought about using your callbacks when the test hits the end and jUnit clears everything from memory. I am still looking for a good recipe to follow for structuring such a test, but I can suggest a few things you could try. The simplest is just to put a sleep() in your test to give things time to process in the other threads. For example:
public void testBindable() throws Exception {
Intent startIntent = new Intent();
startIntent.setClass(getContext(), GpsService.class);
IBinder service = bindService(startIntent);
assertNotNull("Bound to service ",service);
getService().setUpProvider(MOCK_GPS_PROVIDER);
Location demo = new Location(MOCK_GPS_PROVIDER);
demo.setLatitude(22.579937);
demo.setLongitude(88.486805);
getService().setTargetLocation(demo);
TimeUnit.SECONDS.sleep(5);
System.out.println("Test Bindable");
}
This approach is not ideal for a number of reasons. 5 seconds (or whatever you put in) may not be enough sometimes or too much time in other cases.
Alternatively, you can use latches or other mechanisms to detect when your callbacks have executed. However, that approach complicates your code with latches (or you can use mutex locks) that are only used by the test and ideally should not be part of the actual application under test.
The value of MOCK_GPS_PROVIDER shouldn't be "flp" ?
http://developer.android.com/training/location/location-testing.html#TestData

Categories

Resources