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
Related
I have a "Close" button which closes the application.
I have tried 2 methods but they both are very slow.
Finishing the activity:
activity.finish()
Sending Home Page intent (from here)
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
How can I close the activity or send it to the background faster?
Why clicking on the "home" button is much faster than sending ACTION_MAIN intent?
Pressing the HOME Button will call onPause() on the current Activity. Thus it's not closing the app in total but pause it.
Ergo: It is of course much faster even in starting because it keeps the memory.
Now you know how to pause the app instead of closing if you want to have a fast approach.
If you want to close the whole app I would suggest to use finishAndRemoveTask();
Finishes all activities in this task and removes it from the recent
tasks list.
Note: BACK Button will call onDestroy() if you want to have another way. Try out what fits best for your usage.
CODE EXAMPLE
Close app like HOME Button programmatically without a transparent View using a Button:
Button close = findViewById(R.id.myCloseButton);
close.setOnClickListener(view -> {
finishAffinity();
});
I have an activity which is purely transparent (user can't see when it gets open).I open this activity from background service whenever I want to open it. After doing some work, I need to close that activity (not finish()).I want to hide or minimize app. Actually, my app opens up for a second and do someWork after doing someWork, it continues to do remaining work (some uploading process) in background i.e. in onPause.I don't want user to know that something opens & closed immediately.
Currently, I am using below code-
public void minimizeApp() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
Now, Problem is that If user is using some other app eg, playing game or using Whatsapp, When my app get opens over previous app(Game or Whatsapp) and minimizeApp() is called then it minimises my app along with other app opened in background.
I want to minimize only my app.
You can try the following from the activity/context from where you want to minimise the app:
YourActivity.this.moveTaskToBack(true);
I am launching skype with the below code from a button click.
Uri skypeUri = Uri.parse(uri.toString());
Intent myIntent = new Intent("android.intent.action.CALL_PRIVILEGED", skypeUri);
myIntent.setComponent(new ComponentName("com.skype.raider", "com.skype.raider.Main"));
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appContext.startActivity(myIntent);
It gets launched as a seperate activity and my app goes in background. When call is done, and i click on my app from background running apps, it starts from first screen however the call was placed from third screen. Any idea where I am wrong in implementation.
I want my app to go to background as soon as call button is clicked. When i am done with skype call and starts it again from background running apps it should not start from first screen. It should start from that 3rd screen only where i left it.
Thanks
When i click training icon it show the button named Home.
When i click home button it shows the home screen as below.
My application is currently running right, I want to close the application and have to show the home screen when i click the app icon(training).How could i do this..?
Actually i have a button in my layout and i'm showing the home screen with the following code when the button is pressed.
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
It shows the home screen and my service in background has started.
I want to stop all the action from my app to be stopped when i click launcher icon.
My question is how can i stop the activity when my application icon is clicked again.
Sorry for poor english..!
If you application consists only of an Activity then it is already stopped when the user re-clicks the launcher icon (since he had to exit the app).
We need more info if that's not your scenario.
Use it in your activity. When you exit from your app it will stop the action as well as destroy the app. And it will launch new activity when you click your app icon again.
#Override
protected void onDestroy() {
android.os.Process.killProcess(android.os.Process.myPid());
}
Maybe you can check if your service is running, or more dirty with a static int counter. Let the counter increment in your onCreate(). Now you can check for the second start of your apk
Put finish(); on the method onResume().
Of course, if you need to do something else, just put your code before the finish();.
It should do.
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.