I would like to disable the side volume buttons so the only way to control the volume will be from a dedicated activity inside my android app.
I managed to disable it for all my activities by adding the following code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(TAG, "onKeyDown = " + keyCode);
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
return true;
}
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
return true;
}
return super.onKeyDown(keyCode, event);
}
But I don't know how to disable it for the activities i start from my app (for example I start the gallery app)!
I know it is possible as 'Volume Locker' app doing similar stuff.
It is not possible within public APIs to suppress the key events outside of your own Activities, if there is an app that has managed to do it what they are doing would be considered malicious by the platform designers and will get fixed at some point.
Based on the description given for that app (note: I've never used it personally)
Prevent accidental changes to your volume settings, install Volume Locker today.
This app helps prevent against accidental volume changes by confirming the change you made, by either tray notification or a pop up. If you don't approve the change, the volume will be reset within a set amount of seconds... By setting the timeout to "instant", the locked volumes will revert instantly without prompting.
I suspect what that is actually doing is listening for the volume buttons using a similar technique to the one in this answer and just reverting whatever change was made instantly(ish). That would make it seem to the user like the key press did nothing but in reality what happened is the volume changed and then quickly changed back.
Related
currently I am working on a parental control app and I want the child keep from removing the app. For this, I want to lock the device when the child wants to disable my app from device admins (in settings/security) and show an activity to enter the password. But after the activity is shown, I can still use the device by changing through apps (by double square button on the device, I don't know its name).
How can I lock the device such that the user only can see my password asking activity & can't do anything but either enter the password or cancel?
Are you talking about the kiosk mode in android ?
please follow How to implement kiosk mode in android ?
here on Back / Home / Minimize button you can open your dialog for password and if password is true then finish() your current activity.
you can also use KeyEvent for that
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
this.moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
you can find all android keys here
I am making an app in which if power key is pressed twice,The app will perform some task.Please provide me some help.I have made it but its not working.
I am including my class files here,Please correct me if I am going into wrong direction.
Broadcaster
Listener
I don't know how to insert code here it is giving some error so I included links here.I am sorry for that.Please help me.
Android recommends avoiding double clicks and using the long-click besides the normal click.
For the long click use this :
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
// Do something here...
return true;
}
return super.onKeyLongPress(keyCode, event);
}
You also need to add this to the manifest
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
If this doesnt work properly onKeyDown
But I have never actually used this.
My personal opinion though is that you shouldn't hook the power key if you are planning to publish your app because unless it is doing something really an app shouldn't prevent the user from closing the screen
Solved the problem by receiving broadcast on ACTION_SCREEN_ON/OFF.
When the power button is pressed screen turns on/off depending on the fact whether screen is on or off at the time when pressing the power button.
I used this facility and put the logic in code such that if the user presses power button more than 5 times an sms will be sent to the trusted contacts.
This is a little bit tricky.
Background: there is an old GalaxyNexus whose power button is not functioning very well. I have to press hard to turn on/off screen and this is very annoying!
Solution: I have downloaded the newest AOSP and build my own firmware images. I modified two places to handle this problem.
1- In function interceptKeyBeforeQueueing() of PhoneWindowManager, I add the following handling codes:
if(keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
if(event.getAction() == KeyEvent.ACTION_UP && !mPowerManager.isScreenOn()) {
return ACTION_WAKE_UP;
}
}
2- In the status bar source code, I modified the call back when you long-press the home button. I change it from triggering the google assistant to turn off screen (apology to google). In function onTrigger() in SearchPanelView, I modified the code as below:
//startAssistActivity();
vibrate();
PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
if(pm.isScreenOn()) {
pm.goToSleep(SystemClock.uptimeMillis());
}
I have comment out the original code of launching the assist activity and add my code of turning off the screen.
Result: It seems work fine, now I can
Long press the home button and slide to turn off screen
Press volume +/- button to turn on the screen
But, something is abnormal if I disable the lock screen. If the screen lock is set to NONE and I long press the home button and slide to turn off the screen, after I turn on the screen, no matter via volume key or power key, the UI has no reaction when you touch it. Unless you press the home again. It seems that the search panel view is still there and is hijacking my touch events.
I have also tried to modify the code in status bar as following:
mBar.animateCollapsePanels(CommandQueue.FLAG_EXCLUDE_SEARCH_PANEL);
Slog.i(TAG, "onTrigger: post message to turn off screen after 100ms.");
v.postDelayed(new Runnable() {
#Override
public void run() {
PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
if(pm.isScreenOn()) {
pm.goToSleep(SystemClock.uptimeMillis());
}
}
}, 100);
But the problem remains. This issue only happens in the scenario of turning off screen using the "long press the home and slide up" action. Which means to use PowerManager.gotoSleep() method. Is there anything missing here? Any additional handing should be taken?
Is there anybody could give me some hint or inspiration? Thanks a lot!
I am trying to make a simple project that could move the application to background by using
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
moveTaskToBack(true);
}
return super.onKeyDown(keyCode, event);
}
and now I am trying to bring the application to the front, displaying on the screen automatically even though user didn't reopen the application
For example, I set the timeout as 30 seconds, then I close the application (which is moved to background actually), after 30 seconds, the application will automatically move to front and shows text "Time Out". If I am playing games during time out, the game will pause and display the Time Out page, I tried to search for the solutions but the result is quite disappointing. the below are the codes I using now to bring the page to front, but it is only display in the app, unless user reopen the app then onli they can see the Time Out page otherwise they will not know
Intent intent = new Intent("com.lolcash.lol.PopOut");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
is there any other way to do that?
Try to create a service, this will run in background. When you want to bring the activity to front you can start it from the Service.
Another solution would be to display a notification with a message, when the user will tap it you can take him to the app.
There's a situation I have that's related to the Activity stack but I'm new enough to Android dev to not now how to respond to it.
I have an application that works like a stopwatch. When you start it, an ongoing notification goes in the notification tray and remains ongoing until you stop the stopwatch. I've noticed that if my stopwatch is running in the foreground, and I touch the notification, it creates a new instance of my stopwatch's activity and slides the old one off screen. This creates problems with the back button (you press back and the new instance goes away to reveal the old instance). It makes sense why this happens, but I don't want it to happen like this. What can I do to prevent multiple instances of my application from running?
What can I do to prevent multiple instances of my application from running?
In your Intent you use with the PendingIntent for the Notification, add setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); or setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);, whichever behavior fits your needs better.
You could also disable the back button. Not as elegant as CommonsWare solution but nothing wrong with some options!
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
return true;
}
return super.onKeyDown(keyCode, event);
}