Launch activity from background app - android

I have my app running in the background and I want the app to be shown on the top(launched) of the android phone when the code below is ran. (I know the code is ran for sure)
This seems like a simple thing but I spent a couple hours on this site and everyone seems to be suggesting something like this:
Intent intent = new Intent(myActivity.this, myActivity.class);
startActivity(intent);
However, it is not bringing the app to the front and launching it.
I got it to work from a PendingIntent launched from a notification. Which I done by the code below. But I want the app to launch by itself without the user clicking on the notification.
Intent intent = new Intent(myActivity.this, myActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, 0);
notification.setLatestEventInfo(this, "title", "msg", contentIntent);
I also tried:
Intent intent = new Intent("android.intent.action.MAIN");
startActivity(intent);
and flagging the intent:
intent.setFlags(Intent.FLAG_FROM_BACKGROUND);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
But doesn't seem to do anything, any help appreciated.

You should be able to call your own application like this:
Intent intent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.your.package", "com.your.package.MainActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Edit: Forgot to add intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

From what I understand, you want a service that is running in the background and on a certain event, you want your application's activity to come in front i.e. on the users current screen whatever he is doing. It is not advisable to let a background service launch an application without a user's action. The android developer website says
A status bar notification should be used for any case in which a
background service needs to alert the user about an event that
requires a response. A background service should never launch an
activity on its own in order to receive user interaction. The service
should instead create a status bar notification that will launch the
activity when selected by the user.
Hence, do not try to make it launch on its own.

I An not behind my laptop atm so I am nog sure, but I think you have toe pass a context object hand then do context.startactivity(intent);
Sorry for not wel formated I am at my phone atm
Hope It helps

I am clutching at straws here, but you wrote:
MyActivity is launched first, then I either navigate to another app or just hit the home screen to have my app running in the background.
So the situation is that your original Activity is NOT running in the background, when you pressed HOME it might well could have been stopped and destroyed. Your background task remained orphan and MyActivity.this is null at this point.
Try and test what does Log.i(TAG,MyActivity.this); print into LogCat.

I ended up using a pending intent and instead of stright up trying to use a intent.
Something like this: seems a lot more simple.
Intent.send(this, 0, intent);
Thanks.

Also, I’ve seen since compileSdkVersion 29 it's not possible, unless a few restrictions:
The activity started very recently.
The app called finish() very recently.
Through a PendingIntent, but only after a few seconds after the notification was sent.
The app has been granted the SYSTEM_ALERT_WINDOW permission by the user.
...
https://developer.android.com/guide/components/activities/background-starts

Related

Managing FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS in android

Hi i am working on a calling app based on webRtc. Calling activity opens with flag FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS and its working fine.
Intent intent = new Intent(context, CallActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.putExtra(Consts.EXTRA_IS_INCOMING_CALL, isIncomingCall);
context.startActivity(intent);
The problem is When my app is in recent app list and an incoming call comes Call activity opens with above intent . After finishing the CallActivity the app left on the scrreen, it should not, cause CallActivity is triggered when app in in recent app list .
If anyone got the problem let me advise something to acheive it.Let me know in comments below if need more explanation on problem .
The solution was I checked the whether app was in recent or newly opened.
I case of app was in recent stack i send it to back stack again by moveTaskToBack.
moveTaskToBack(true);

startActivity() doesn't work when app is in background (In this special case)

I'm trying to bring my app from background to foreground. In onHandleIntent() of my custom IntentService class, I have:
Intent intent = new Intent();
intent.setClass(getApplicationContext(), MainActivity.class); // Also tried with "this" instead of getApplicationContext()
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
Now this code works at first sight but I found a scenario where it doesn't work. If you have the app opened and you put it to background via home button and execute startActivity() within ~5 second, there will be a delay before your app will come to foreground. This is a known implementation and you can find the topic discussed on stackoverflow. In this scenario, the app succeeded in coming from background to foreground.
If you repeat the same experiment above, but instead of waiting for the app to come to foreground, go browse (scroll, swipe, etc) around your phone (I was browsing around the google playstore). The result is that startActivity() will get called but the app will not come to the foreground.
I'm not asking for a solution but more of an explanation on why this is happening. Is this intended behavior?
Use the context of your class.
For instance :
Intent intent= new Intent(context, other.class)
Instead of getapplicationContext()
Use the code :
private void startMenuActivity() {
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
the below code works for me,
val login = Intent(applicationContext, SignInActivity::class.java)
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
login.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
applicationContext.startActivity(login)

Send application to back & bring to front

I want to develop an application where it will start on particular time & close on particular time. Similar like Alarm but not alarm application.
As per my thinking I will start an service when application first started service will check current time & particular timing to match the condition & when condition is to close application it will simply send application to background so that service will be running & when condition for wake up occurs service will bring application front.
Is this possible? If yes please give an example links or anything helpful.
Also I am trying to understand the service but it's little bit complex for me so if you have link which will help me to understand the service it will be very helpful. Thank You.
Problem Solved:
/*To close the activity/
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
MainActivity.this.finish();
OR Just finish activity;
MainActivity.this.finish();
/** To start the activity*/
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName(this, MainActivity.class);
i.setComponent(cn);
startActivity(i);
It worked for me still I if any one have better solution please post below.
Thank You
You can create a BroadcastReceiver to listen for time changes, and at your specified times, create an Intent to launch or to close your main Activity.

Intent is very slow to launch a new Activity :(

I have this piece of code for an Intent:
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(new ComponentName(packToLaunch, nameToLaunch));
startActivity(i);
This basically launches a new activity based on the package name that I pass to it. Sometimes, it takes up to 5 seconds to launch this new Activity. Is there any way to speed this process up? It even takes this long when I have an app that is still running. Please help...
It looks like Android intentionally delays launch of activity from service right after you press HOME button. (When using BACK button everything is OK.) There was even issue posted https://code.google.com/p/android/issues/detail?id=4536 however it got obsolete.
I tried to search actual implementation of the delay in Android source, but failed. You might want to check the following question as it states pretty same issue and gives some more insights: Starting an activity from a service after HOME button pressed without the 5 seconds delay

Sending a running application to background programmatically

Is it possible to send a activity into background programmatically in android?
I am creating a prank application that plays funny sounds after a specified time (input by the user). And I don't want the application to be visible when playing that sound and also the display should be dark.
Yes.
You can use either:
boolean sentAppToBackground = moveTaskToBack(true);
if(!sentAppToBackground){
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);
}
More information here: http://developer.android.com/reference/android/app/Activity.html#moveTaskToBack(boolean)
Or simply:
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);
According to Romain Guy a Android Framework Engineer, "You cannot simulate a press on the Home key.". So beware...
Check: http://osdir.com/ml/Android-Developers/2010-03/msg01887.html
Updated this answer according to: moveTaskToBack(true) returns false always
This function ultimately works for you
moveTaskToBack(true)
Or download source code . Android minimize app programmatically
Maybe play the sound from a service instead?
To expand on the answer by #ns476, the reason you should play it from a service is that any Activity that is no longer in the foreground can be killed at any time by the OS. Please review the activity lifecycle.

Categories

Resources