We have been working on developing service for android platform.
In our service we need to send GPS data (Lat and Long) of device to some external REST service afte
Yes you can implement a background service that it will almost never be killed. But you have to declare it to run in the foreground. you can see what Android Developer site says, by referring to this url(http://developer.android.com/guide/components/services.html) also in this article (http://developer.android.com/guide/components/processes-and-threads.html) they say,
There are five levels in the importance hierarchy and the different types of processes in order of importance (the first process is most important and is killed last):
A process that is required for what the user is currently doing. A process is considered to be in the foreground if any of the following conditions are true:
Generally, only a few foreground processes exist at any given time. They are killed only as a last resort—if memory is so low that they cannot all continue to run. Generally, at that point, the device has reached a memory paging state, so killing some foreground processes is required to keep the user interface responsive.
So you have to start your service in the foreground. In order to do this you have implement the service as below.
public class MyForegroundService extends Service {
@Override
public void onCreate() {
super.onCreate();
//your code goes here
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
keepServiceAlive();
//your code goes here
return(START_NOT_STICKY);
}
private void keepServiceAlive() {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this).setContentTitle(getString(R.string.app_name))
.setContentText("Hello")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.build();
startForeground(Notification.FLAG_ONGOING_EVENT, notification);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.w(getClass().getName(), "Got to stop()!");
stopForeground(true);
}
}
Thanks and gud luck..
In Manifest file,
<service android:name=".MyService"></service>
MyService.java
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
// your code here
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Intent it = new Intent(MyService.this, MyService.class);
getApplication().startService(it); // If service will destroy, Start the service again
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
to run the service, add this to your Activity,
Intent it = new Intent(getApplication(), MyService.class);
getApplicationContext().startService(it);