zergy
zergy

Reputation: 93

Launch activity from background app

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.

Upvotes: 5

Views: 30342

Answers (6)

javier_domenech
javier_domenech

Reputation: 6253

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

Upvotes: 2

zergy
zergy

Reputation: 93

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.

Upvotes: 0

ilomambo
ilomambo

Reputation: 8350

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.

Upvotes: 0

Bram P.
Bram P.

Reputation: 96

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

Upvotes: 6

Maikel Bollemeijer
Maikel Bollemeijer

Reputation: 6565

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

Upvotes: 0

Alabhya
Alabhya

Reputation: 490

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.

Upvotes: 0

Related Questions