Start Launcher Selection Programmatically / Android - android

I'm developing an industrial app that's running effectively in a KIOSK mode - that is users must not be able to exit it.
To achieve this I've simply made the app a launcher/home screen replacement. This works very effectively and so far seems to be working as a method of preventing people getting out.
The only problem I have is that if I'm not careful we're going to end up with bricked devices where we can't get back to a normal launcher application.
What I'm looking for is a method of programmatically presenting the Android Launcher Selection Dialog.
Android seems to do this on it's own when you first launch a launcher, but I can't figure out a way of doing it programmatically.

What I'm looking for is a method of pro grammatically presenting the Android Launcher Selection Dialog.
Intent.createChooser() returns an Intent that will launch a chooser (which I think is what you mean by "Android Launcher Selection Dialog") for a given Intent. So, create an Intent for ACTION_MAIN and CATEGORY_HOME, wrap that in Intent.createChooser(), and call startActivity() on the resulting Intent.

If I understand this question right you don't want to get the user out of your app if he don't want it?
I would do it like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return true;
}
Now button clicks will be ignored for the activity. Now you only have to make a menu point for the exit.

Related

What is base intent?

from : http://developer.android.com/guide/topics/manifest/activity-element.html
android:relinquishTaskIdentity
Whether or not the activity relinquishes its task identifiers to an
activity above it in the task stack. A task whose root activity has
this attribute set to "true" replaces the base Intent with that of the
next activity in the task.
What is base intent here?
The base intent is the root intent that initially launched your app.
Most common one is probably the one that any app has when responding to the touch on the app icon. The LAUNCHER intent.
But it can be a custom one, for example, when you respond to custom scheme/url. But here is the trick and how relinquishTaskIdentity can be useful :
Say you start your app with the launcher icon. Your base intent is now the default one.
Now, say your app is completely killed (or you've backed with the hardware icon until your app closes) and you use a custom scheme/url to open your app, at this point the base intent is not the default one. It's the one generated from the scheme/url you clicked on and may contain custom data also. Now if you just close your app with the home button and reopen it, you will just resume where you were at. But if you back, back, back... with the hardware button until your app closes there is the trick : reopening it from the recent apps/multitasking view will reuse the base intent to open it and in this particular case it will still be your custom scheme/url intent and this can be very annoying.
Why annoying? Say the scheme/url your user clicked on was used to auto login and he succeeded: you don't really want to process this url/intent again just because your user backed up until its app closed and reopened it via recent apps/multitasking view, right?
Use relinquishTaskIdentity! This is very dependent to your setup and how your app is configured as for the Activities versus Fragments, but here is an example:
In the particular case I mentioned with the auto login via a link you could have an Activity that is dedicated and is only responding to scheme/url. This same activity should be different than the one flagged as LAUNCHER. and using the property relinquishTaskIdentity="true" on it will make the base intent become any subsequently shown activity via that one.
So what's the benefit in our case? The user can't enter the app with a custom scheme/url generated intent anymore unless he/she really clicked on one supported by your app.

In my own custom android home screen, how do I catch the home button?

I know that android does not allow us to catch the home button press; however, I have my own built home screen replacement app and want to just know when the button was pressed to allow animations.
So, my app has just one activity as I do most of my "activities" as canvas drawing so I can have complete control over the visual aesthetics of my app. The problem is if the user navigates to a different page within the app and presses home, nothing happens, since the app is technically already in the same activity.
I want to know when the button is pressed so I can then animate/navigate my app back to the main screen. Also, I know I could accomplish this by having separate real activities however that won't work for what I'm trying to do.
You need to add <category android:name="android.intent.category.HOME"/> to your activity's intent filter in your app's Android.xml
You can take a look at "Home" app in Android SDK samples. They can be downloaded using these instructions.
//overriding method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
//The Code Want to Perform.
}
});
You can detect when the home button is pressed because onPause() is always called. You are not allowed to override the home button, but the following should work
#Override
protected void onPause()
{
super.onPause();
//code here
}
Of course the problem with using this is that it will be called even if the user is not quitting the app(ie. If they are sent to another application through an intent)
That is the closest that you are going to get to a onHomeButtonPressedListener there are really no ways to do this
Home key press Event Listener

How to prevent user from closing application - Android 4.0.3

So my question is: If is possible to prevent user from closing application.
Problem is because i can't hide action bar and i use tablet only for work time registration. So if someone press home button or back button is that not acceptable.
So i wonder if i could somehow handle onclose event?
Is it possible to open application in fullscreen (with no action bar)?
You cannot prevent user from closing application if he presses home button. That's the whole idea of it. Otherwise, you could leave user trapped in your app with no means to exit but to reboot it's device.
Short answer is no.
Long answer is that you can make it quite difficult for the user to close you app. Some of the tricks that can be used are: reopen you app as soon as it closes , disable keys like back and power and finally disable the home button
A user will always be able to close an application, otherwise there would be programs abusing it and causing problems, but there are steps you can make to better handle it being closed.
For example, if you have a remote service running that can check if the application is running, and there can be various ways to know, then it could fire off an intent to start the application again.
One way to know if a program is alive is to have it periodically call the service, basically doing a heartbeat check, and if it hasn't been called in some period of time, which should be 2 or 3 times larger than the expected check-in period, then fire off the intent.
There are other steps that may work, if you detect that the home button was pressed, but I would need to think through those steps. I think it depends on your expectations though, as trapping someone in your program would be really bad.
try this...
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Ref-> How to hide the title bar for an Activity in XML with existing custom theme
to prevent the user .......
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK || keyCode==KeyEvent.KEYCODE_HOME){
// pass some msg ......
return true;
}
return super.onKeyDown(keyCode, event);
}
So here is my solution that works...
First you install trial version of SureLock application. Then in that app disable action bar. And thats it.
If you want to view again action bar you will need HideBar app
All of that works only on rooted devices...

Open Browser as Intent but don't keep it on the Activity stack

I'm having some problems with understanding the activity stack and the behaviour of how it affects my app.
Upon clicking a button it starts an Intent which opens the browser. When I'm in the Browser and I press the home button I land onto the homescreen. Now if I start my app again via launcher it opens the browser instead of my app. How can I circumvent opening the browser upon launching my app?
Right now, the code to open an url looks like this:
private void openUrlExternal(String url) {
Intent openUrlIntent = new Intent(Intent.ACTION_VIEW);
openUrlIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
openUrlIntent.setData(Uri.parse(url));
startActivity(openUrlIntent);
}
Am I using the wrong flags? If so, what flags do I have to use?
Thanks in advance!
Try like this:
openUrlIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
openUrlIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
That should disassociate the browser task from your own which means when you re-launch yours it should go to your Activity instead of the browser.
However it also depends on where you are calling openUrlExternal() from. If you call this when your activity launches it is still going to take you back to the browser, but if you call this from an event listener (i.e. Button click) then it shouldn't get called when you re-launch your app.
I don't think the accepted answer is exactly correct. It depends on what you intend (no pun intended, heh) to do.
Using Intent.FLAG_ACTIVITY_NEW_TASK it means that the launched activity is completely separate from the launching one. In particular, you can switch to the old activity with the Apps button without exiting the new one.
Using Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET means that the user will be returned to the previous activity when it's launched from the apps drawer.
In both cases launching the app again will get you to the previous activity. The main difference will be whether both activities (or just the last one) are shown in the app switcher.

Weird Home button behavior

I'm experiencing kind of strange behavior of my application after hard Home button is pressed.
When you press Home, everything is OK - my app goes to the background, showing Home screen. But if you try to choose my app in the main menu or in the list of last tasks it behaves like it was not started before and does not show the last activity you were on - it just starts from scratch, namely, shows the splash screen and starts next corresponding activities. Moreover, old activities of this app remain on the activities stack, and previous instance of the app is not terminated - so if you press Back for a few times you'll just run into those activities which were undoubtedly started during the previous session of work with my app. Splash screen activity is filtered by "android.intent.action.MAIN" filter and "android.intent.category.LAUNCHER" category.
The strange thing is that all of that happens despite the fact that I do not intercept any Back key hits, or override any onPause or onResume methods. What's happening contradicts with my understanding of Android app lifecycle - I was sure that when you hit Home an app just goes to the background, and when you choose it in the menu later - it just unwinds and does not start anew. (Of course, unless stuff like that is stated in the app manifest or corresponding methods are overridden or something else).
I also checked it for some other lifecycle events - such as changing orientation or flipping hard keyboard out - and none of those led to such strange results. It appears that the problem occurs when you try to start the app from main menu or menu of last applications.
I hope you will be able to help me. Any advice on what to pay attention to or where to search for solution would be really great.
Regards, Alex
You need to set android:launchMode="singleTask" in your LAUNCHER activity in your manifest file.
For more info on the launchMode attribute see here
Note that:
The default mode is "standard".
and:
Every
time there's new intent for a
"standard" activity, a new instance of
the class is created to respond to
that intent.

Categories

Resources