DontPanic
DontPanic

Reputation: 2416

How to switch between Android apps programmatically

I want to switch quickly between two running Android apps, a Client and a Server, for debugging purposes. The two are connected by a socket connection. Ideally, I'd like to add a button to both to toggle to the other (preserving the connection) so I can easily see what's going on at both ends.

Here's what I tried:

Upvotes: 4

Views: 2732

Answers (1)

DontPanic
DontPanic

Reputation: 2416

I finally figured out how to switch between apps programatically (preserving their state). In each app, I used 'getTaskId()' to get its task id and saved it to a file on /sdcard/.

Then, in each app, on a button click, I call

void switchTask()
{
  int tid;
  ActivityManager am;
  am = (ActivityManager)Ctx.getSystemService( Context.ACTIVITY_SERVICE );
  tid = getPkgTaskId();  // read task id of *other* app from file
  am.moveTaskToFront( tid, 0, null );
}

Note: These need permissions:

  • READ_EXTERNAL_STORAGE
  • WRITE_EXTERNAL_STORAGE
  • REORDER_TASKS

Upvotes: 2

Related Questions