6

I want to display service status,if it is running or stopped. I am using the below code but it shows "stopped" before starting service. when service is started it shows "running". and when it is stopped again it shows "running" only. Am I making any mistake in setting sharedPreference status. In mainActivity

  onCreate()
  {
   checkServiceStatus() ;
  }

  private void checkServiceStatus() 
{
    Toast.makeText(getApplicationContext(),"in enableControls", Toast.LENGTH_LONG).show();
    boolean isServiceRunning = AppSettings.getServiceRunning(this);     
    if (isServiceRunning)
    {
        Toast.makeText(getApplicationContext(),"service running", Toast.LENGTH_LONG).show();    
    } 
    else 
    {   
        Toast.makeText(getApplicationContext(),"service stopped", Toast.LENGTH_LONG).show();        
    }
}


  public class AppSettings 
  {
 public static final String SERVICE_STATE = "isServiceRunning";
 public static boolean getServiceRunning(Context context){
         SharedPreferences pref = context.getSharedPreferences(GPSLOGGER_PREF_NAME, 0);

         return pref.getBoolean(SERVICE_STATE, false);
 }

 public static void setServiceRunning(Context context, boolean isRunning){
         SharedPreferences pref = context.getSharedPreferences(GPSLOGGER_PREF_NAME, 0);
         SharedPreferences.Editor editor = pref.edit();

         editor.putBoolean(SERVICE_STATE, isRunning);
         editor.commit();
 }
Manasi
  • 348
  • 1
  • 4
  • 14
  • check this http://stackoverflow.com/questions/600207/android-check-if-a-service-is-running –  Apr 15 '13 at 10:02

3 Answers3

10
public class Receiver extends BroadcastReceiver {       
    @Override
    public void onReceive(Context context, Intent intent) {    
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (YourService.class.getName().equals(service.service.getClassName())) {
                //your service is running
            }
        }
        //your service is not running
    }
}
Dirk
  • 10,668
  • 2
  • 35
  • 49
3
 ActivityManager manager = (ActivityManager)context.   getSystemService(Context.ACTIVITY_SERVICE);//use context received in broadcastreceiver

for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
 {

     if (MyService.class.getName().equals(service.service.getClassName())) {
                         //running 
        }
    else{
          //not running 
        }

 }
Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43
Sharad Mhaske
  • 1,103
  • 9
  • 19
-4
 public static boolean CheckServiceConnectivity(Context mContext) 
     {
        ConnectivityManager connec = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
                || connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED)
            return true;

        return false;

    }
ishu
  • 1,322
  • 1
  • 11
  • 15