I would like to handle the volume up and volume down keyEvents in my App Widget.
It really should happen in the app widget or a Service, not in an Activity (the homescreen should be displayed).
But as far as i know there don't seems to be a possibility to react with the user from an app widget or service, so i can't get the keyEvents.
So my Question is if I'm wrong and there is a possibility to get the keyEvents or maybe one to make the acitivity invisible?
If by "Widget" you mean "app widget", then, no, you cannot respond to key events from an app widget.
I have found a trick. It's not really the best, but in my case I can use it.
With assigning the themes
android:theme="#android:style/Theme.Dialog"
or
android:theme="#android:style/Theme.Translucent.NoTitleBar"
in the Android Manifest, it's possible to have an Activity that looks like an Dialog or which is transparent. So the home screen is visible and in the Activity I can get the keyEvents.
Although it isn't possible that the user can interact with the home screen during the activity is running, but in my case this isn't needed necessarily.
Related
I want to know if the user clicks on the Home button (like listen to Home button).
Because I saw that is there now way to do that, I tough about an idea.
Maybe if I will create a launcher I will be able to handle to Home button...
But since I don't want to really create a launcher, so I want to create a launcher, that opens the previous one.
How can I create such a launcher? Or is it possible to use BroadcaseReceiver to listen to Home button?
HOME button can not be monitored nor reacted to and there is no workaround.
As for the launcher:
First, you would need to fetch the ResolveInfo for the current launcher and keep its package name somewhere, like SharedPreferences.
Later, the user would have to accept your launcher as the default launcher. After that, you should make your launcher's onCreate() method's only job to create an intent which will open the previously saved package and then immediately call finish() on your launcher.
However, I am pretty sure you will stumble upon some problem along the way, as this is Android. Anyway, be my guest to try and post the result here, the concept is really interesting.
I'm developing a presentation style application for HoneyComb Tablets. At one stage the tablet may be passed around a room for people to interact with. If possible I would like to prevent malicious users from navigating away from the current activity.
So far I have overwritten the onBackPressed() to prevent finishing the activity but users can still press the other buttons on the status bar and also leave the app via notifications that pop up.
Any suggestions or possible solutions?
Thanks
1. Make your activity full screen.
2 Use an alarmManager to trigger your activity from a service on a regular interval say 2or3 second (only if your activity is not foreground). For this use a boolean variable and store it using sharedPreference. this value will be true in onReume and false in onPause or in onStop or in onDestroy. And then only start your activity from your service if the boolean variable is false. Now if your user will press the Home button then AlaramManager kick start your activiy again.
3 Make a special button for finishing your service and activity and for cancel the alarmManager.
As far as I know there is no way to capture the home button press, so this is not possible. Not to mention, it would be a bad ui design decision by the dev team. The home button is there so every Android user has a standard way to exit apps. There would be some extremely malicious apps if there was a way to make the user unable to exit an app.
I'm developing a similar application that runs in a "kiosk" fashion for retail stores. When the application launches, it programmatically hides the system bar so you cannot exit. The system bar is restored when the tablet is rebooted. It requires root/su however.
So I'm trying to do what many people have tried before: create an app that does not respond to the Home button. I've looked at many of the similar questions posted here on SO, but none of worked the way I wanted them to.
One thing I tried was making my app essentially another launcher. (Note: a little amount of user input is required to make it work.)
From my application's Manifest:
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.HOME" />
I also disabled the back button and made the launch mode "singleInstance."
I think Toddler Lock did something similar, but my implementation does not behave exactly the same way. Using my implementation, my app exists as the default home launcher indefinitely including after the application has been exited. Is there any way to declare the same behavior in a place other than the Application Manifest where it can be turned on temporarily or and turned off when the app is exited?
Car Home also does a similar thing and actually does it better than Toddler Lock. I'm not sure how it does it (maybe it has more permissions since it is a native app), but it manages to do the same thing without requiring the user to accept the alternate Launcher or choose the app as the default Launcher. Anyone have any idea on how it does it?
hackbod is essentially correct. I have gotten much of the desired behavior by
Make a "capture home key" activity
as described in the question. This
is not the main activity of the
program.
In the manifest, disable it.
In the app, enable the "capture home
key" activity when you want the home
capture to happen, and disable it
when you want to exit.
The only question is what the capture home key activity should actually do. In my case, I needed it to just go to the start of the app... so it manufactures a CATEGORY_HOME intent, tests that it resolves correctly, and if so forwards on to the app. If it doesn't resolve correctly, it notifies the user, waits for the user to be ready, and then uses that intent. This way if the user chooses your app but doesn't make it default, he'll get prompted again.
To disable this after the user has enabled your app as the home app, disable that activity with PackageManager.setComponentEnabledSetting(). Note this implies that the activity that is overriding home should not be the main activity of your app, or else upon disabling it the user won't be able to return to your app.
CarHome is very different -- when Android is in a different UI mode (in a car for car mode or on a desk dock for desk mode), then a different Intent will be launched when the user presses home so that they can have a different "home" in that environment. If you are not writing a car mode home screen, you should not be using this.
Hi I have application with more than 20 activities.I want to close my app when Home button is pressed.
There is no notion of "close my app" in Android. Android will get rid of your activities, followed by your process, after some period of inactivity by the user.
You could use the launchMode and clearTaskOnLaunch flags on your root activity from your AndroidManifest.xml:
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
When you again start your app, all activities will be killed.
You don't want to do System.exit() -- that's not how the Android Activity Lifecycle normally works (read this also).
What you should do is move the App to the background with moveTaskToBack(). Android will then keep your app running in the background, and kill it if it's unused and something needs its resources later.
If you want it to close all of the open Activities when your App is no longer visible, you can set noHist = "True" for all of the child activities (leave the main activity with noHist = "False", though). This will make it where instead of reopening your application on the last Activity they were on, it will open it on the "main" activity (i.e. it will be as if they just restarted the app).
Anyhow, read through the following answers for more information: Close application and launch home screen on Android
I have the same problem. Im writing a banking app and am required, by contract, to log off the user (or exit) when the app is put into background. There are obvious security concerns there.
There are a couple of ways Im looking to do this:
1. Intercept home button (and back button for the root activity) key press events to call logoff and/or finish()
2. In the onStop() method, for every activity, detect whether the activity is being stopped due to a new activity being show - if not, assume app is being put to background so logoff and/or finish()
The first may not work if a notification is brought to the front then the user clicks home (I havent investigated yet). Or maybe there are other ways for an app to be put into the background without pressing these buttons
The second way sounds messy & difficult to maintain
Id welcome any other ideas
Drapes
I know android has been designed this way, but it seems naive to think that apps wouldnt want an applicationOnStop event
Hi guys what I understood is that u need to know when app goes in background and how to detect it and if I am wrong plz correct me----
The user can go in background if ur app does not provide any way by pressing Back key or Home Key.
You need to use methods "dispatchKeyEvent(KeyEvent event)" to get home key event or back key event and after getting the event you can execute your task.
you can even restrict user from pressing any key but u can not control the home key.
When a user presses a shortcut on the screen and that shortcut starts up an activity, how can I get the location of where the shortcut is on the screen?
I have the same question for a widget. When a user has a widget on the screen and pressing it fires off the setOnClickPendingIntent how can I know the location on the screen where the widget is?
In both scenarios there is no View to run View.getLocationOnScreen.
When a user presses a shortcut on the screen and that shortcut starts up an activity, how can I get the location of where the shortcut is on the screen?
You can't, sorry.
When a user has a widget on the screen and pressing it fires off the setOnClickPendingIntent how can I know the location on the screen where the widget is?
You can't, sorry.
The only way to get any of that would be to write your own home screen, where you are handling the shortcut presses and app widget taps.
EDIT: The way the QuickContactActivity achieves the effect described in the comments is via getSourceBounds(), a method on Intent. This Rect may or may not be available on any given Intent, and therefore any code looking to use it should be able to cleanly react to an Intent that has no such value. I can see where RemoteViews, the basis for app widgets, use it. I am having a bit more difficulty seeing under what other circumstances the Launcher application uses it. Bear in mind that not all home screens may elect to use it. I apologize for my erroneous original answer.