I am working on a lock screen replacement for my media player, and I am having trouble with one aspect.
The lock screen itself is a new activity that is launched by my media service whenever the screen turns off. The problem is that the Activity uses the applications context to launch, which causes it to bring the app to the front after the user unlocks.
I have tried using the services Context to start the activity, but I believe this does not work because the service itself is linked to the application.
Below is the code in the service that launches the activity:
Intent mLock = new Intent(context, LockScreen.class);
mLock.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mLock);
Does anyone have any suggestions on how I can launch this activity and keep my app in the background?
Explanation. Generally, every activity is having its UI(Layout). But if a developer wants to create an activity without UI, he can do it.
If both application have the same signature (meaning that both APPS are yours and signed with the same key), you can call your other app activity as follows: Intent LaunchIntent = getActivity(). getPackageManager(). getLaunchIntentForPackage(CALC_PACKAGE_NAME); startActivity(LaunchIntent);
Now in the onResume() method, check if we already have the value of prevStarted in the SharedPreferences. Since it will evaluate to false when the app is launched for the first time, start the MainActivity (Which we want to appear just once) and set the value of prevStarted in the SharedPreferences.
Foreground services perform operations that are noticeable to the user. Foreground services show a status bar notification, so that users are actively aware that your app is performing a task in the foreground and is consuming system resources.
In the manifest set the task affinity of the lock screen. That should sort you out without using FLAG_ACTIVITY_MULTIPLE_TASK.
After a lot of searching I was able to get the results I desired by adding a FLAG_ACTIVITY_MULTIPLE_TASK
. This allows the lock activity to start and end without bringing the parent application to the front.
I will continue to test this solution and post any drawbacks I find. If anyone else would like to chime in on this feel free...I know this flag has drawn criticism in the past.
Thanks, Josh
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With