Reputation: 1531
In my project I need to create a service in android. I am able to register the service like this :
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<service android:enabled="true"
android:name=".ServiceTemplate"/>
<activity
android:name=".SampleServiceActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I am calling this service inside an activity like below:-
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent service = new Intent(getApplicationContext(), ServiceTemplate.class);
this.startService(service);
}
But if I kill the current activity , the service is also destroyed. I need this service always running in the background. What I need to do? How do I register the service? How do I start the service?
Upvotes: 15
Views: 68314
Reputation: 352
to complete what @abhinav said : if you use onBind() you dont need to use onStartCommand() and vice versa :
in fact, If a component calls bindService() to create the service and onStartCommand() is not called, the service runs only as long as the component is bound to it. After the service is unbound from all of its clients, the system destroys it. To illustrate: onStartCommand() methos is called when your Service begins to do its work. onCreate() has completed and it is ready to get to doing what needs to be done. and if you use onBind() method, you tie the Service to the lifespan of, for example, an Activity. If the Activity completes then the Service is allowed to be released and can itself finish. The Service will last as long as there is something still bound to it.
Upvotes: 0
Reputation: 1087
you can create background service and call that by AlarmManager
1- you have to create a BroadcastReceiver class for calling by AlarmManager
public class AlarmReceiver extends BroadcastReceiver
{
/**
* Triggered by the Alarm periodically (starts the service to run task)
* @param context
* @param intent
*/
@Override
public void onReceive(Context context, Intent intent)
{
Intent i = new Intent(context, AlmasService.class);
i.putExtra("foo", "AlarmReceiver");
context.startService(i);
}
}
2-you have to create a IntentService class for calling by AlarmReceiver
public class AlmasService extends IntentService
{
public Context context=null;
// Must create a default constructor
public AlmasService() {
// Used to name the worker thread, important only for debugging.
super("test-service");
}
@Override
public void onCreate() {
super.onCreate(); // if you override onCreate(), make sure to call super().
}
@Override
protected void onHandleIntent(Intent intent) {
context=this;
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
String val = intent.getStringExtra("foo");
// Do the task here
Log.i("MyTestService", val);
}
}
3- you have to add AlarmReceiver as receiver and AlmasService as service on manifest
<service
android:name=".ServicesManagers.AlmasService"
android:exported="false"/>
<receiver
android:name=".ServicesManagers.AlmasAlarmReceiver"
android:process=":remote" >
</receiver>
4-now you can start service and call AlarmManager on MainActivity
public class MainActivity extends AppCompatActivity
{
public static final int REQUEST_CODE = (int) new Date().getTime();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scheduleAlarm();
}
public void scheduleAlarm()
{
// Construct an intent that will execute the AlarmReceiver
Intent intent = new Intent(getApplicationContext(), AlmasAlarmReceiver.class);
// Create a PendingIntent to be triggered when the alarm goes off
final PendingIntent pIntent = PendingIntent.getBroadcast(
this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup periodic alarm every every half hour from this point onwards
long firstMillis = System.currentTimeMillis(); // alarm is set right away
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
// First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
// Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY
alarm.setRepeating(AlarmManager.RTC_WAKEUP, firstMillis, (long) (1000 * 60), pIntent);
}
}
Upvotes: 5
Reputation: 9082
Here is a semi-different way to keep the service going forever. There is ways to kill it in code if you'd wish
Background Service:
package com.ex.ample;
import android.app.Service;
import android.content.*;
import android.os.*;
import android.widget.Toast;
public class BackgroundService extends Service {
public Context context = this;
public Handler handler = null;
public static Runnable runnable = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "Service created!", Toast.LENGTH_LONG).show();
handler = new Handler();
runnable = new Runnable() {
public void run() {
Toast.makeText(context, "Service is still running", Toast.LENGTH_LONG).show();
handler.postDelayed(runnable, 10000);
}
};
handler.postDelayed(runnable, 15000);
}
@Override
public void onDestroy() {
/* IF YOU WANT THIS SERVICE KILLED WITH THE APP THEN UNCOMMENT THE FOLLOWING LINE */
//handler.removeCallbacks(runnable);
Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "Service started by user.", Toast.LENGTH_LONG).show();
}
}
Here is how you start it from your main activity or wherever you wish:
startService(new Intent(this, BackgroundService.class));
onDestroy()
will get called when the application gets closed or killed but the runnable just starts it right back up. You need to remove the handler callbacks as well.
I hope this helps someone out.
The reason why some people do this is because of corporate applications where in some instances the users/employees must not be able to stop certain things :)
https://i.sstatic.net/rdUBn.png
Upvotes: 18
Reputation: 1444
Try to start the service in separate thread, so that when you will destroy your activity the service will not be affected. It will run without any interruption. Also, in the service return Service.START_STICKY
from onStartCommand(intent, flags, startId)
to make sure that the service is re-created if it is killed by the system (Android OS).
Upvotes: 4
Reputation: 12444
override this method:
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
Upvotes: 1
Reputation: 1007494
But if am kill the current activity the service also is killing. I need this service always running in the background. What I need to do?
If by "kill the current activity" you mean that you are using a task killer, or Force Stop from within the Settings app, your service will be stopped. There is nothing you can do about that. The user has indicated they do not want your app to run any more; please respect the user's wishes.
If by "kill the current activity" you mean you pressed BACK or HOME or something, then the service should keep running, at least for a little while, unless you call stopService()
. It will not keep running forever -- Android will eventually get rid of the service, because too many developers write services that try to be "always running in the background". And. of course, the user can kill the service whenever the user wants to.
A service should only be "running" when it is actively delivering value to the user. This usually means the service should not be "always running in the background". Instead, use AlarmManager
and an IntentService
to do work on a periodic basis.
Upvotes: 6