问题
I have a weblink on the android tablet that when clicked, runs Chrome full screen.
In that website, I can click a link and open my application (developed in Android Studio). But once that app is opened, I want it to immediately send focus back to Chrome. I've seen people say on this forum there is a way to switch tasks, but they didn't provide code or links on how to do it.
Or disabling my app from the task manager (but still have it run) would be good too I maybe?
What I am trying to do is send start and stop commands to the application from the Chrome website. i.e. 1st time, it opens and 'starts' the app doing something. App stays open and does it's thing until it receives a 'stop' command from the website. Process repeats.
I have tried the intent's getdata within the onResume function, but since the app doesn't send 'focus' back to the website after it is called, onResume isn't called the next time Chrome sends data to it. The app is 'invisible', so when it is opened, the user continues within the Chrome website without even knowing an app has been opened. But like I said, even though they are continuing within Chrome, the actual focus is now on the other app. It's kind of weird.
So, this is a little different in that I have to switch back to an already opened Chrome link, not simply just Chrome..if you know what I mean.
Any help on how to do this programmatically within my app is appreciated.
回答1:
When your app opens for the start command, kick off a background service and tell it to start, then finish() the activity if you have one - this will return control to the OS which will show the previous activity - chrome
When your app receives the stop command then do the same - send the stop command to your background service, then finish
Android has a couple of service types explained here. Usually you can completely avoid having any activities by starting a service directly from another app if that is an option, but because I think you are using deeplinking functionality from the browser then I suggest an activity with no UI which catches that link and kickstarts the service as explained above
回答2:
found this, which works:
super.onBackPressed();
来源:https://stackoverflow.com/questions/41526316/android-switch-back-to-chrome-from-app