Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Activity without bringing Application to front

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?

like image 308
Josh Avatar asked Jul 17 '11 13:07

Josh


People also ask

Is it possible to have an activity without UI layout?

Explanation. Generally, every activity is having its UI(Layout). But if a developer wants to create an activity without UI, he can do it.

How do I start an activity in another application?

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);

How do you start an activity only once the app is opened for the first time?

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.

What is foreground activity?

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.


2 Answers

In the manifest set the task affinity of the lock screen. That should sort you out without using FLAG_ACTIVITY_MULTIPLE_TASK.

like image 72
Philip Pearl Avatar answered Nov 20 '22 02:11

Philip Pearl


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

like image 36
Josh Avatar answered Nov 20 '22 02:11

Josh