0

I am creating a small app that needs to be able to start completely separate apps automatically after registering certain triggers.

I have tried the following two methods to start the apps:

  • start activity

    Intent i = ctx.getPackageManager().getLaunchIntentForPackage("package.name"); ctx.startActivity(i);

  • shell command

    adb shell monkey -p package.name -c android.intent.category.LAUNCHER 1

The problem is that the first method only works if the screen is on and unlocked and the command can bring the app in the foreground. The second method, even though it starts the apps, the apps do not start their sub activities.

To put this in context, say I want to start a VPN app. The first method doesn't do anything if the screen is off or the phone is locked. The second command does start the app even with the phone locked and screen off, but the app does not start it's own VPN service/activity until I turn the screen on. In essence, the apps don't do anything (even though they do start) until the screen is on.

Any ideas how I can start an app and make sure it actually does what it normally does when it is started by actual human interaction, instead of just sitting idle?

The app will be used on a rooted Android 7.0 phone.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1969903
  • 810
  • 13
  • 26
  • The activity will in stopped state when the screen is off so this looks a normal behaviour to me but I may be wrong about this. If you are able to start the other application's activity and if the functionality does not require a human interactions why not put it in a service and then launch the service on `onCreate` of the activity. Just an assumption though! – Vinod Maurya Jun 27 '18 at 06:19
  • I agree that it is seems normal behavior too, but I still need a way to override this behavior. The app's functionality appears to start automatically once the app is in the foreground / screen is on. Regarding your suggestion, I am not that well versed in the topic. Can you provide some reading material? Thanks! – user1969903 Jun 27 '18 at 07:40
  • Normally: Newer Android versions require a special notification if app is in background (eg device is off etc). Also, Android will also determine if it wants to speed up the cpu for it or not and you can't do anything about that of what I'm aware of so you might get slow cpu. And too, you would need to know when the display is off which is another problem https://stackoverflow.com/questions/9477922/android-broadcast-receiver-for-screen-on-and-screen-off or always have an app running in bg. You've got battery concerns and many things to look into. Rooted: Find way around this like system app. – CmosBattery Jun 27 '18 at 12:10
  • As a system app, I still don't seem to be able to start the apps correctly, but perhaps I am messing something up. As a workaround, since the apps seem to start if the screen is on, I will try and force the screen to stay on while the apps load. It isn't ideal, but I suppose I can live with my screen turning on for a couple of seconds every now and then. If it works I will post it as an answer. – user1969903 Jun 28 '18 at 11:01
  • Still no luck. I managed to turn the screen on but some apps just don't run if the screen isn't turned on by myself. I'm curious how apps like Tasker do it. – user1969903 Jul 02 '18 at 04:55

0 Answers0