Skip to content

Commit

Permalink
Add option to force-stop app before starting
Browse files Browse the repository at this point in the history
The previous commit introduced:

    scrcpy --start-app=name

By adding a '+' prefix, the app is stopped beforehand:

    scrcpy --start-app=+name

This is especially useful to start it on a new virtual display:

    scrcpy --new-display --start-app=+firefox
  • Loading branch information
rom1v committed Oct 19, 2024
1 parent 76931f4 commit 11eca93
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/scrcpy.1
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,10 @@ Default is "lalt,lsuper" (left-Alt or left-Super).
.BI "\-\-start\-app " name
Start an Android app, by its exact package name or the start of its name (case-insensitive).

Add a '+' prefix to force-stop before starting the app:

scrcpy --new-display --start-app=+firefox

.TP
.B \-t, \-\-show\-touches
Enable "show touches" on start, restore the initial value on exit.
Expand Down
4 changes: 3 additions & 1 deletion app/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,9 @@ static const struct sc_option options[] = {
.longopt = "start-app",
.argdesc = "name",
.text = "Start an Android app, by its exact package name or the start "
"of its name (case-insensitive).",
"of its name (case-insensitive).\n"
"Add a '+' prefix to force-stop before starting the app:\n"
" scrcpy --new-display --start-app=+firefox",
},
{
.shortopt = 't',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,11 @@ private int getActionDisplayId() {
}

private void startApp(String name) {
boolean forceStopBeforeStart = name.startsWith("+");
if (forceStopBeforeStart) {
name = name.substring(1);
}

List<DeviceApp> apps = Device.findApps(name);
if (apps.isEmpty()) {
Ln.w("No app found for name \"" + name + "\"");
Expand All @@ -604,7 +609,7 @@ private void startApp(String name) {
}

Ln.i("Starting app \"" + app.getName() + "\" [" + app.getPackageName() + "] on display " + startAppDisplayId + "...");
Device.startApp(app.getPackageName(), startAppDisplayId);
Device.startApp(app.getPackageName(), startAppDisplayId, forceStopBeforeStart);
}

private int getStartAppDisplayId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private static List<DeviceApp> findStartingByName(PackageManager pm, List<Applic
}

@TargetApi(Build.VERSION_CODES.O)
public static void startApp(String packageName, int displayId) {
public static void startApp(String packageName, int displayId, boolean forceStop) {
Intent launchIntent = FakeContext.get().getPackageManager().getLaunchIntentForPackage(packageName);
if (launchIntent == null) {
Ln.w("Cannot create launch intent for app " + packageName);
Expand All @@ -283,6 +283,9 @@ public static void startApp(String packageName, int displayId) {
launchOptions.setLaunchDisplayId(displayId);

ActivityManager am = ServiceManager.getActivityManager();
if (forceStop) {
am.forceStopPackage(packageName);
}
am.startActivity(launchIntent, launchOptions.toBundle());
}

Expand Down

0 comments on commit 11eca93

Please sign in to comment.