i have a simple service need to run in background.below is the code for the service.i want to run the code in onStartCommand repeatedly simply for test purpose i displayed toast.but Toast also calling only once
import android.app.IntentService;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
/**
* An {@link IntentService} subclass for handling asynchronous task requests in
* a service on a separate handler thread.
* <p>
* TODO: Customize class - update intent actions and extra parameters.
*/
public class WiFiCheck extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
/**
* Handle action Foo in the provided background thread with the provided
* parameters.
*/
private void handleActionFoo(String param1, String param2) {
// TODO: Handle action Foo
throw new UnsupportedOperationException("Not yet implemented");
}
/**
* Handle action Baz in the provided background thread with the provided
* parameters.
*/
private void handleActionBaz(String param1, String param2) {
// TODO: Handle action Baz
throw new UnsupportedOperationException("Not yet implemented");
}
}
but onStartCommand is calling only once getting toast only once.
i used
return START_STICKY;
starting service as below
startService(new Intent(this, WiFiCheck.class));
but still no use.any help
Toast will be twice and more called if you start again service
e.g.
startService(new Intent(this, WiFiCheck.class));
startService(new Intent(this, WiFiCheck.class));
new Intent
is an intent which is in onStartCommand(Intent intent
Short answer
Call StartService
as much times as you want the toast to show up.
About returning START_STICKY(Why doesn't returning START_STICKY show your toast twice?)
Returning START_STICKY
at the end of onStartCommand()
method lets your service start again(onStartCommand()
is called again) when your service is KILLED(by some reasons like system resource depletion). So there is no relevence between return START_STICKY;
and your goal.
Solution to reach your goal
Like the Kalyzunyu's answer, just call StartService()
twice to show your toast twice. It does not instantiate your Service twice, but calls your onStartService()
twice. So be free to call it again.
Refer here.
Or if you want to show the toast every 10 seconds until it is stopped try this.
public class WiFiCheck extends Service
{
private Thread thread;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
startForeground(1, new Notification());
thread=new Thread(new Runnable(){
@Override
public void run()
{
// TODO: Implement this method
while(true)
{
Toast.makeText(WiFiCheck.this, "Service Started", Toast.LENGTH_LONG).show();
try
{
Thread.sleep((long)10000);
}
catch (InterruptedException e)
{}
}
}
});
thread.start();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
/**
* Handle action Foo in the provided background thread with the provided
* parameters.
*/
private void handleActionFoo(String param1, String param2) {
// TODO: Handle action Foo
throw new UnsupportedOperationException("Not yet implemented");
}
/**
* Handle action Baz in the provided background thread with the provided
* parameters.
*/
private void handleActionBaz(String param1, String param2) {
// TODO: Handle action Baz
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onDestroy()
{
// TODO: Implement this method
thread.stop();
super.onDestroy();
}
}
This code below is equivalant to above.
In Activity
new Thread(new Runnable()
{
@Override
public void run()
{
while(!isInterrupted()){
startService(new Intent(MainActivity.this, WiFiCheck.class));
Thread.sleep(10000L);
}
}
}).start();
Equivalent code #2:
public class WiFiCheck extends IntentService
{
public WiFiCheck() {
super("WiFiCheck");
}
@Override
protected void onHandleIntent(Intent intent)
{
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
startForeground(1, new Notification());
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
}
To add words, starting a service does not mean it is repeatedly called by the system, but rather it can live longer without UI. Actually its context is continued until you manually call stopSelf()
or any component calls stopService()
on your service.
YES.., You just have to return START_STICKY
as KYHSGeekCode
suggested. It was down voted, but it is correct answer. So i up voted it back. Thank you