Reputation: 31
I've implemented an android service that start on boot :
MyService.java
package com.example.testservicelogging;
import com.example.testservicelogging.MyBroadcastReceiver;
import android.app.PendingIntent;
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.widget.Toast;
public class MyService extends Service implements LocationListener {
LocationManager lm;
PendingIntent pendingIntent;
LocationListener locationListener;
public MyService() {
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//TODO do something useful
//Toast.makeText(getBaseContext(), "Got in!!!", Toast.LENGTH_SHORT).show();
System.out.println("Got in");
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
//---use the LocationManager class to obtain locations data---
lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
Intent i = new Intent(this, MyBroadcastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(
this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
//---request for location updates using GPS---
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
6000,
5,
pendingIntent);
return START_STICKY;
}
@Override
public void onDestroy() {
//---remove the pending intent---
lm.removeUpdates(pendingIntent);
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
Toast.makeText(this, "onLocationChanged", Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(this, "onProviderDisabled", Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(this, "onProviderEnabled", Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
Toast.makeText(this, "onStatusChanged", Toast.LENGTH_LONG).show();
}
}
also a BroadcastReceiver
class :
package com.example.testservicelogging;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent t=new Intent(context, MyService.class);
t.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(t);
String locationKey = LocationManager.KEY_LOCATION_CHANGED;
String providerEnabledKey = LocationManager.KEY_PROVIDER_ENABLED;
//Toast.makeText(context, "INSIDE!!!", Toast.LENGTH_SHORT).show();
System.out.println("INSIDE");
if (intent.hasExtra(providerEnabledKey)) {
if (!intent.getBooleanExtra(providerEnabledKey, true)) {
Toast.makeText(context,
"Provider disabled",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context,
"Provider enabled",
Toast.LENGTH_SHORT).show();
}
}
if (intent.hasExtra(locationKey)) {
Location loc = (Location)intent.getExtras().get(locationKey);
Toast.makeText(context,
"Location changed : Lat: " + loc.getLatitude() +
" Lng: " + loc.getLongitude(),
Toast.LENGTH_SHORT).show();
//do something with the coordinates returned
}
}
}
I'm facing some problems :
Thank you very much for the help !!!
Upvotes: 3
Views: 1669
Reputation: 330
Make sure either one of these are added to the manifest, based on what you need:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Upvotes: 1
Reputation: 95568
You are requesting location updates and providing a PendingIntent
that should be broadcast when the location updates occur. However, your service also implements LocationListener
so it looks like maybe you are a bit confused. I'm not sure exactly what you are trying to do here, but to start I would just let your service get the callbacks directly. In that case, you don't need the BroadcastReceiver
and you should change this:
//---request for location updates using GPS---
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 6000, 5, pendingIntent);
to this:
//---request for location updates using GPS---
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 6000, 5, this);
This will cause the location service to call back periodically on the methods that you've already implemented in your service class (onLocationChanged()
, onProviderEnabled()
, etc.)
Also, the parameters you've provided for minTime and minDistance are very very small. minTime of 6000 is 6 seconds and minDistance is 5 meters. This will cause the GPS to be on constantly draining the battery, so you might want to reconsider those parameters.
I suggest you try that and add some logging to see what happens. I would suggest using the logging framework (ie: Log.v()
and Log.e()
, etc.) and look at the logcat instead of trying to use toasts. Toasts are pretty unreliable for debugging.
Also, the location service implementation is different on all devices. Each manufacturer provides their own implementation and they vary widely in reliability, robustness and operational characteristics. There is no way to reliably insist on GPS updates at specific intervals or at specific times. All you can do is provide suitable hints to the operating system about what you would like to have. The location framework will call you back whenever it wants to, regardless of the parameters you've specified. You just need to understand that and code accordingly.
If you want to get updates every 15 minutes, you can either specify a 15 minute minTime (which would be a value of 15 * 60 * 1000) OR you can schedule an wakeup alarm with the alarm manager every 15 minutes and when the alarm goes off you can request location updates iimmediately (although it can take some time for you to get them).
Hopefully I've provided you with enough material so that you can make some progress here. Feel free to add comments if you need more help.
Upvotes: 1