Note that there are some explanatory texts on larger screens.

plurals
  1. POLaunching Intent from service causes crash
    primarykey
    data
    text
    <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></p> <p>I have now set up a service to run my runCommand() function (see below) however when it comes to actually launching the intent from within run command my app force closes with:</p> <pre><code>E/AndroidRuntime (5448): FATAL EXCEPTION: Thread-931 E/AndroidRuntime (5448): 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 (5448): at android.app.ContextImpl.startActivity(ContextImpl.java:864) E/AndroidRuntime (5448): at android.content.ContentWrapper.startActivity(ContextWrapper.java 276) E/AndroidRuntime (5448): at packagename.MainService.runCommand(MainService.java:164) </code></pre> <p>So my question is how will I go about launching the required intent form a service?</p> <p>my service class is:</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"); } } }; 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; } </code></pre> <p>}</p> <p>And my service is started using:</p> <pre><code>Intent intentService = new Intent(this, MainService.class); this.startService(intentService); </code></pre> <p>in my main activity</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload