title: Run a task in background always in android
body: <p>I have a problem that I made a service which executes a task in background after every 5 seconds, for this I made a receiver in manifest and autostarter class but it is not excuted in my app. Means the service is not running in the app. I don't know why? Please suggest me for the right answer.</p>
  
  <p><strong>Android Manifest:</strong></p>
  
  <pre><code>&lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt;
         &lt;receiver android:name="com.halosys.TvAnyTime.ServiceAutoStarter"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
          &lt;/intent-filter&gt;
    &lt;/receiver&gt;
          &lt;activity android:name=".MainScreen"
  
                    android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar"&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;activity android:name=".IntroVideo"  android:label="@string/app_name" android:theme="@style/CustomTheme" android:screenOrientation="landscape"/&gt; 
             &lt;activity android:name=".WelcomeScreen1" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait"/&gt; 
    &lt;activity android:name=".IntroFaceBookScreen" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait"/&gt; 
    &lt;/application&gt;
  
      &lt;uses-permission xmlns:android="http://schemas.android.com/apk/res/android" 
          android:name="android.permission.INTERNET"&gt;&lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/&gt;
  
  &lt;uses-permission android:name="android.permission.READ_INPUT_STATE"&gt;&lt;/uses-permission&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
   &lt;uses-permission android:name="android.permission.READ_PHONE_STATE"/&gt; 
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
    &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_MOCK_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;uses-sdk android:minSdkVersion="3" /&gt; 
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>ServiceAutoStarter:</strong></p>
  
  <pre><code>public class ServiceAutoStarter extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Intent myIntent = new Intent(context, ServiceTemplate.class);
            myIntent.putExtra("extraData", "somedata");
            context.startService(myIntent);
        }
      }
  </code></pre>
  
  <p><strong>ServiceTemplate.java:</strong></p>
  
  <pre><code>public class ServiceTemplate extends Service {
  
      Runnable refresher;
      public static int HashesInPattern=32;         // 32 64byte hashes in the pattern
       public static int BytesInHash =64; 
       static byte[] hashPattern;
       ArrayList&lt;byte[]&gt; finalHashafterXor = new ArrayList&lt;byte[]&gt;();
       byte[] finaldatafile = new byte[2097152];
       byte[] finalhash;
       InputStream is;
       byte[] bytesafterXor,bytes;
       String strLine;
     @Override
     public IBinder onBind(Intent intent) {
        return null;
     }
  
     @Override
     public void onCreate() {
        //code to execute when the service is first created
     }
  
     @Override
     public void onDestroy() {
        //code to execute when the service is shutting down
     }
  
     @Override
     public void onStart(Intent intent, int startid) {
        //code to execute when the service is starting up
         final Handler handler = new Handler();
  
  
          refresher = new Runnable() {
           public void run() {
             // some action
               try{
                   Encrypt();
                   Toast.makeText(getApplicationContext(), "Hello World", Toast.LENGTH_SHORT).show();
               }
               catch(Exception e)
               {
                   e.printStackTrace();
               }
               handler.postDelayed(refresher, 2000);
           }
         };
  
     }
  </code></pre>
  
  <p>Thanks in advance.</p>