Reputation: 335
I'm actually developping an Android application that allows a parent to track his child. The problem i'm facing is that the GPS Location needs to be executed every N seconds, but whenever i force close the application, or restart the mobile the service does not restart.
Here is my LocationService:
public class LocationService extends Service
{
private LocationListener listener;
private LocationManager manager;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate()
{
super.onCreate();
Toast.makeText(this, "Service.onCreate()", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy()
{
super.onDestroy();
Toast.makeText(this, "Service.destroy()", Toast.LENGTH_LONG).show();
if(this.manager != null)
//noinspection MissingPermission
this.manager.removeUpdates(this.listener);
}
@Override
public int onStartCommand(Intent intent, int flag, int startId)
{
super.onStartCommand(intent, flag, startId);
//Here end the code to execute
Toast.makeText(this, "Service.onStartCommand()",Toast.LENGTH_LONG).show();
this.listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.e("started", "service running");
play();
Toast.makeText(LocationService.this, location.getLongitude() + " " + location.getLatitude(), Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
};
this.manager = (LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
//noinspection MissingPermission
this.manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000,0, this.listener);
return START_NOT_STICKY;
}
@Override
public boolean onUnbind(Intent intent)
{
// TODO Auto-generated method stub
Toast.makeText(this, "Service.onUnbind()", Toast.LENGTH_LONG).show();
return super.onUnbind(intent);
}
}
My Broadcast Receiver :
public class BootIntentReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Intent serviceIntent = new Intent(context, LocationService.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(serviceIntent);
}
}
My Manifest.xml :
<receiver android:name=".activity.service.BootIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".activity.service.LocationService"
android:exported="false" />
The permission for the boot :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Whenever i boot the device nothing happens :/
Thank you for your help guys !
Upvotes: 1
Views: 39
Reputation: 2220
Did you try using fully qualified package names for your service in the manifest?
e.g. If the service is in package com.package.activity.service
Use <receiver android:name="com.package.activity.service.BootIntentReceiver">
instead of just <receiver android:name=".activity.service.BootIntentReceiver">
And put a log or Toast in your BootIntentReceiver
, just to be doubly sure
Upvotes: 1