Call App From Background - Appium/Android - android

I need to send an app to background, return home and call it from background again in Android, with Appium.
I've alredy tried
driver.runAppInBackground(seconds);
and
driver.launchApp();
but they re-launch the application, don't call it.
How should I handle this?
Thank you in advance.

Try this way, to start home screen:
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
And from background service after some time or event, you can start you main activity again the same way:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example", "com.example.MyExampleActivity"));
startActivity(intent);
You can start any application activity using intents.

I did something like this just now, but the difference is I called another app(so basically putting the app in background). The way i runned the app from background is long press home button with adb shell input keyevent --longpress 3, then used .click() on the app i want to run from background. You can use UIAutomatorViewer to find the element :). Hope this helps

Related

Android old intent keeps relaunching

Hi in my app I have a dialog that asks if they would like to turn on my accessibility service if it is off. If they hit okay it launches an intent to open to accessibility like this
Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivityForResult(intent, 0);
This works fine however if they hit home from accessibility and then open my app again it doesn't show the dialog or anything but opens directly to accessiblity everytime. The only way to get around it is to hit back till I'm back at my home screen then reopening the app no longer opens to accessibility.
Why does it keep launching my old intent unless I back out? Why can't I hit back and then it not launch the intent again
Thanks for any help
Use flag FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivityForResult(intent, 0);
However if the user launch your app from the history list, it will still show up.
I think the problem is that your activity is still waiting for a result.
If so, calling forceFinish() in your onResume() should fix the problem.
http://developer.android.com/reference/android/app/Activity.html#finishActivity%28int%29

Start Activity on mobile restart

I Want to design an App. Which will start on mobile restart.
I am able to start my activity by using "android.intent.action.BOOT_COMPLETED" intent
but "SAMSUNG NOTE" has facility to restart mobile not needed to switch off and then on
here i am facing problem my app is not starting on "restart"
Problem Solved for now by removing code which is commented.
Intent startMainActivity = new Intent(context,MyActivity.class);
startMainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMainActivity);
/* Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMain);*/
I was using commented code to launch home screen after my app is launched

how to maximize the application on an event?

I have minimized my app using this..
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
The app is waiting for some event while it is in minimized state.How can i return to the app activity when some broadcast event occurs?
i can access the minimized activity from the the broadcast receiver class(using an object passed to it earlier) but it is running in the background of home screen.
and does android kill minimized apps eventually?
Intent homeIntent = new Intent(this, HomeActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(homeIntent);
this will start the HomeActivity, if the activity still in memory, it will just bring it to front.
You can start the activity again using the Intent.
Example :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
startActivity(intent);
If you want to maximize the application you need user interface in that case you can add your application in notification bar, once user click on notification bar you can open in your word can maximize the same. Adding your application in to notification bar you can get many tutorials :
Notification

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

Categories

Resources