title: Launching already running activity from service intent
body: <p>RE: <a href="https://stackoverflow.com/questions/15396585/move-back-to-app-after-launching-another-app">Move back to App after launching another App</a>
  and <a href="https://stackoverflow.com/questions/15412846/launching-intent-from-service-causes-crash">Launching Intent from service causes crash</a></p>
  
  <p>I now have the terminal launching correctly from my service, however my next step is to, after the service has launched the terminal intent to relaunch the apps main activity.</p>
  
  <p>I am doing this using:</p>
  
  <pre><code>public void backtoEmplayer(){
      Intent intenti = new Intent(MainService.this,MainActivity.class);
      intenti.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intenti.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
      startActivity(intenti);
  }
  </code></pre>
  
  <p>So that it will simply bring forward the main activity which should still be running (As the only two things ever launched on this device will be my app and the terminal).
  However when I run the above code I get the following error:</p>
  
  <pre><code>E/AndroidRuntime (1859): FATAL EXCEPTION: Thread-931
  
  E/AndroidRuntime (1859): android.util.AndroidRuntimeException: Calling startActvitiy() from outside of an Activity context requires the FLAT_ACTIVITY_NEW_TASK flas. Is this really what you want?
  
  E/AndroidRuntime (1859): at android.app.ContextImpl.startActivity(ContextImpl.java:864)
  
  E/AndroidRuntime (1859): at android.content.ContentWrapper.startActivity(ContextWrapper.java 276)
  
  E/AndroidRuntime (1859): at packagename.MainService.backtoEmplayer(MainService.java:187)
  </code></pre>
  
  <p>What I do not understand is that fact that I am using the FLAT_ACTIVITY_NEW_TASK flag but it still does not work.</p>
  
  <p>My complete service class now looks like:</p>
  
  <pre><code>public class MainService extends Service {
  
      boolean copied = false;
      private String mHandle;
      private static final int REQUEST_WINDOW_HANDLE = 1;
  
      @Override
      public IBinder onBind(Intent arg0) {
          return null;
      }
  
      @Override
      public void onCreate() {
  
      }
  
      @Override
      public void onDestroy() {
  
      }
  
      @Override
      public void onStart(Intent intent, int startId) {
          Thread usbUpdateThread = new Thread() {
              public void run() {             
                  while (true) {
                      while (!copied) {
                          try {
                              Thread.sleep(180000);
                          } catch (InterruptedException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                      }
                      runCommand("chmod -R 777 /media/cdrom/");
                      copied = false; 
                  }
              }
          };
          Thread InternetThread = new Thread() {
              public void run() {
                  while (true){
                          try {
                              Thread.sleep(3600000);
                          } catch (InterruptedException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                          runCommand("php update.php");
  
                  }
              }
          };
          Thread CheckThread = new Thread() {
              public void run() {
                  while (true) {
                          try {
                              Thread.sleep(300000);
                          } catch (InterruptedException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                          runCommand("sh check.sh");
  try {
  
  
  Thread.sleep(10000);
                      } catch (InterruptedException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                      backtoEmplayer();
                      }
  
              }
          };
  
          InternetThread.start();
          CheckThread.start();
          usbUpdateThread.start();
      }
  
  
      public boolean runCommand(String command) {
          Intent intent = new Intent("jackpal.androidterm.RUN_SCRIPT");
          intent.addCategory(Intent.CATEGORY_DEFAULT);
          intent.putExtra("jackpal.androidterm.iInitialCommand", command);
          intent.putExtra("jackpal.androidterm.window_handle", MainActivity.mHandle);
          startActivity(intent);
  
  
          return true;
      }
      public void backtoEmplayer(){
  
  
          Intent intenti = new Intent(MainService.this,MainActivity.class);
          intenti.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          intenti.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
          startActivity(intenti);
      }
  
  }
  </code></pre>