Intent is very slow to launch a new Activity :( - android

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

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

start an activity without show it

I was trying to start activity from a service without showing it to the user, keep it work in background, I was searching a lot about that, and I found two ways to do that
by starting the activity then start the home main screen like this :
// this is for lunch the app
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.example.some");
// this is for going back
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// lets do it !
startActivity(LaunchIntent);
startActivity(startMain);
by putting a going back function in the activity itself, like this :
context.getActivity().moveTaskToBack(true);
BUT, in these two ways I have problems
in the first one, if the user was on another app, this operation will close his app and get him to home , more than that, some times the my activity not started but just be in the back without working i.e. if there was a song it isn't played
in second one, when the my activity started a black screen will appear for a second before it back to the home or previous user app
So , simply , this is what I want :
I want a behaviour equal to : the user open my app then he press back button, but without show that the app started unless he see the background apps
how to do that ?
For that, you can use an android Service.
See docs - http://developer.android.com/guide/components/services.html

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.
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

Launching activity from another application not displaying the activity, but it is running

I've done a lot of searching for the last day and haven't found anything that seems to match the problem I'm having.
(For reasons I won't go into) the app is divided into two separate apks. Each have activities. Only the "core" apk has a MAIN activity that is launched from the Android Launcher. The "plugin" apk has activities that only exist to be called from the first apk's activities and does not have a Launcher icon.
The issue I'm having is that when I create an intent to launch an activity from the "plugin" apk, it does the "launching new activity" sliding animation but immediately bounces back. But it is actually launching the activity because I'm seeing log statements in logcat coming from the new activity. I'm not getting any exceptions and it seems to be working other than the fact I'm not seeing the activity on the screen.
I've tried creating the intent in both of the following ways:
Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.setClassName("com.test.plugin", "com.test.plugin.PluginActivity");
startActivity(myIntent);
and
Intent myIntent = new Intent();
myIntent.setComponent(new ComponentName("com.test.plugin", "com.test.plugin.PluginActivity"));
startActivity(myIntent);
But both result in the same thing happening as described above.
It bounces back means the plugin activity has some problem in launching.

Android Launching or bringing to front another application via Intent

Im having trouble getting this to work, here´s a quick overview of the idea.
First, I cant change the logic behind this, it was a specific requirement from the customer, I realize that with any tool such as AnyCut it could be bypassed but that doesnt matter really.
My customer offers a suite of apps, the idea is that all applications bellonging to the suite would be launched from a "Dashboard app", so that I only show the Dashboard app in the main launcher and not all app icons.
Lets take two Apps to get the idea solved. The Dashboard App (A) and the Recieving App (B).
I want to establish an intent filter (I think) on app B so that whenever I go into app A, and click the app B icon the app will be either launched or started from where it let of (brought to front).
Is this even possible? If so, how can I do it? I managed to get it to launch by specifically launching one activity in the app using:
Intent i = new Intent();
i.setClassName("PACKAGE_NAME","SPECIFIC_CLASS");
startActivity(i);
But that isnt the behaviour that I want, as it always starts app B in the same spot.
Thanx in advance,
Stefano
Edit: Added some new information. I was taking a look at the DDMS.
If I launch the application from scratch through the main Android launcher the intent is exactly the same as when I leave the home button pressed and then only bring the app to front, what ever activity im in. So I was trying to reproduce, unsucsesfully until now, this intent.
INFO/ActivityManager(1292): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.package/.uiPackage.Activity}
This is how AnyCut does it
Intent { act=android.intent.action.VIEW flg=0x10000000 cmp=com.example.package/.uiPackage.Activity bnds=[125,242][235,360]}
Any idea how I could go about creating that exact same intent? I cant even find that flag in the Intent API.
Figured it out, this is how I did it.
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setAction("android.intent.action.VIEW");
i.setComponent(ComponentName.unflattenFromString("com.example.package/com.example.package.activityName"));
startActivity(i);
I'm not quite sure I'm following the expected results you want to see, but the following would launch the app from the dashboard and remove the dashboard from the activity stack leaving the selected app running:
Intent i = new Intent();
i.setClassName("PACKAGE_NAME","SPECIFIC_CLASS");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
I believe this should start the app as if you were starting any other app.
Please add more information on your logic if this is not what you are looking for.
I think that when you switch activities android's default action is to sort of pause or hold the activity in its state the user left it in last. I know there is a way to make it so that the state is not saved when switching activities but I cant remember it off the top of my head.

Categories

Resources