title: Invoking service class using Alarm Manager
body: <p>I have developed an application to monitor network status after every minute.... i am using Service component to monitor the network status and storing the network details into the Database.I  am invoking the service class from a Broadcast Receiver using AlarmManager class.... i am repeating the AlarmManager in-order to invoke the service class after every minute.... but unfortunately service class is not getting invoked exactly after every minutes... instead it gets invoked for some time and it will stop... i have attached the source code here.... </p>
  
  <p>Please let me know how to run the Service class continuously in the Background using AlarmMagerclass after every minute...</p>
  
  <p>Thanks in advance .. ;-)</p>
  
  <p>My Service class</p>
  
  <pre><code>public class NetworkInfoService extends Service implements LocationListener{
  
      private String strNetworkInfo;
      private LocationManager locationManager;
      private NetworkDetailsVO networkDetailsVO = new NetworkDetailsVO();
      private NetworkInfoDatabaseHelper networkInfoDatabaseHelper;
  
      @Override
      public void onCreate() {
          super.onCreate();
  
          ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
          TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
  
          NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
  
          if(networkInfo!=null &amp;&amp; networkInfo.isConnected()){
              String networkState = getNetworkStateString(networkInfo.getState());
              String stateString = networkInfo.toString().replace(',', '\n');
              strNetworkInfo = String.format("Network Type: %s\n Network State: %s\n\n %s",
                      networkInfo.getTypeName(),networkState,stateString);
          }
  
          locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
  
          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
          /*locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 300f, this);*/
  
          telephonyManager.listen(new SignalStrengthDetector(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
  
          networkDetailsVO.setNetworkInfo(strNetworkInfo);
          networkDetailsVO.setPhoneNumber(telephonyManager.getLine1Number());
          networkDetailsVO.setOperatorName(telephonyManager.getNetworkOperatorName());
          networkDetailsVO.setDateTime(new Date().toString());
  
          Log.i("Network DetailsFrom Service", networkInfo.toString());
  
          networkInfoDatabaseHelper = new NetworkInfoDatabaseHelper(getApplicationContext());
          networkInfoDatabaseHelper.saveRecord(networkDetailsVO);
  
      }
  
      @Override
      public void onStart(Intent intent, int startId) {
  
          ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
          TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
  
          NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
  
          if(networkInfo!=null &amp;&amp; networkInfo.isConnected()){
              String networkState = getNetworkStateString(networkInfo.getState());
              String stateString = networkInfo.toString().replace(',', '\n');
              strNetworkInfo = String.format("Network Type: %s\n Network State: %s\n\n %s",
                      networkInfo.getTypeName(),networkState,stateString);
          }
  
          locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
  
          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
          /*locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 300f, this);*/
  
          telephonyManager.listen(new SignalStrengthDetector(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
  
          networkDetailsVO.setNetworkInfo(strNetworkInfo);
          networkDetailsVO.setPhoneNumber(telephonyManager.getLine1Number());
          networkDetailsVO.setOperatorName(telephonyManager.getNetworkOperatorName());
          networkDetailsVO.setDateTime(new Date().toString());
  
          Log.i("Network DetailsFrom Service", networkInfo.toString());
  
          networkInfoDatabaseHelper = new NetworkInfoDatabaseHelper(getApplicationContext());
          networkInfoDatabaseHelper.saveRecord(networkDetailsVO);
  
  
      }
  
  
      /*
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
          return Service.START_STICKY;
      }*/
  
      private String getNetworkStateString(NetworkInfo.State state){
  
          String stateString = "unknown";
  
          switch(state)
          {
          case CONNECTED: 
              stateString = "connected";             
              break;
  
          case CONNECTING:        
              stateString = "connecting";    
              break;
  
          case DISCONNECTED:      
              stateString = "disconnected";  
              break;
  
          case DISCONNECTING:     
              stateString = "disconnecting";  
              break;
  
          case SUSPENDED:         
              stateString = "suspended";              
              break;
  
          default: 
              stateString = "unknown";     
  
              break;
  
          }
  
          return stateString;
      }
  
  
      @Override
      public void onLocationChanged(Location location) {
  
          networkDetailsVO.setLatitude(location.getLatitude());
          networkDetailsVO.setLongitude(location.getLongitude());
          networkDetailsVO.setAccuracy(location.getAccuracy());
      }
  
  
      @Override
      public IBinder onBind(Intent intent) {
  
          return null;
      }
  
  
      @Override
      public void onProviderDisabled(String string) {
  
  
      }
  
      @Override
      public void onProviderEnabled(String provider) {
  
          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, this);
      }
  
      @Override
      public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
  
  
      }
  
  
      private class SignalStrengthDetector extends PhoneStateListener{
  
          @Override
          public void onSignalStrengthsChanged(SignalStrength signalStrength) {
              super.onSignalStrengthsChanged(signalStrength);
              networkDetailsVO.setSignalStrength(signalStrength.getGsmSignalStrength());
          }
  
      }
  
  
  
  
  }
  </code></pre>
  
  <p>My Broadcast Receiver class (it is registered for android.intent.action.BOOT_COMPLETED  intent)</p>
  
  <pre><code>public class NetworkInfoListner extends BroadcastReceiver{
  
      @Override
      public void onReceive(Context context, Intent intent) {
          PendingIntent service = null; 
          Intent intentForService = new Intent(context.getApplicationContext(), NetworkInfoService.class);
          final AlarmManager alarmManager = (AlarmManager) context
                          .getSystemService(Context.ALARM_SERVICE);
          final Calendar time = Calendar.getInstance();
          time.set(Calendar.MINUTE, 0);
          time.set(Calendar.SECOND, 0);
          time.set(Calendar.MILLISECOND, 0);
          if (service == null) {
           service = PendingIntent.getService(context, 0,
                              intentForService,    PendingIntent.FLAG_CANCEL_CURRENT);
                  }
  
                  alarmManager.setRepeating(AlarmManager.RTC, time.getTime()
                          .getTime(), 60000, service);
  
      }
  </code></pre>
  
  <p>My manifest file</p>
  
  <pre><code> &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.networkinfodemo_v_03"
          android:versionCode="1"
          android:versionName="1.0" &gt;
  
          &lt;uses-sdk
              android:minSdkVersion="8"
              android:targetSdkVersion="17" /&gt;
  
          &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
          &lt;uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /&gt;
          &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
          &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
          &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
          &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
  
          &lt;application
              android:allowBackup="true"
              android:icon="@drawable/ic_launcher"
              android:label="@string/app_name"
              android:theme="@style/AppTheme" &gt;
              &lt;activity
                  android:name="com.example.networkinfodemo_v_03.activity.NetworkInfoActivity"
                  android:configChanges="orientation|keyboardHidden|screenSize"
                  android:label="@string/app_name"
                  android:theme="@style/FullscreenTheme" &gt;
                  &lt;intent-filter&gt;
                      &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                      &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
                  &lt;/intent-filter&gt;
              &lt;/activity&gt;
  
              &lt;service android:name="com.example.networkinfodemo_v_03.service.NetworkInfoService" android:enabled="true"&gt;
              &lt;/service&gt;
  
              &lt;receiver android:name="com.example.networkinfodemo_v_03.broadcastreceiver.NetworkInfoListner" &gt;
                  &lt;intent-filter&gt;
                      &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
                  &lt;/intent-filter&gt;
              &lt;/receiver&gt;
          &lt;/application&gt;
       &lt;/manifest&gt;
  </code></pre>
  
  <p>please let me know what is the error...</p>