How to receive Broadcast on PowerButton - android

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.

Related

How to view all system generated Intents?

Is there a way to view all intents that are generated by the Android OS's at any moment but maybe filtered by the activity ? Specifically I am testing the onHoverListener - I want to know if my activity is throwing away the hover motionevent or whether none is being generated (system not capable/ some other problem)
Ideally I would like a log of all intents given to my activity - but some other trick is also fine.
Android intents are , capable for starting a new activity , service , complete any action i.e send email , click photo , fetch data from a content provider etc .
Capturing any intent of that kind , you need to have intent filters registered to your activity with the same actions , as that of system intents .[The framework will pick your actvity/app if the same intent is fired and hence you may be able to intercept those intents if the user prefers to] This may be little too much as there will be so many of the actions declared for different android components .But some popular examples are sending sms , picking a contact , send mail .
Please refer to this for more info :http://developer.android.com/guide/components/intents-filters.html
Events and Intents are different all together , all events generated by your app will go the event handler queue of the UI thread .
So in order to intercept you should set appropriate event listeners to your activity components .
Try using onTouch instead .
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//Button Pressed
}
if(event.getAction() == MotionEvent.ACTION_UP){
//finger was lifted
}
return false;
}`

Android - What is the correct way of turning off screen

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!

How to override the side volume keys outside my android application

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.

Catching when the user pressed the Home Button

I would like to know when the user pressed the home button while he was running my app.
BUT:
The problem is that I don't want to edite the existing code.
meaning I don't want to add logic to the existing activities onPause() method.
The only solution I found was to add a service to the application which listens to the Log detecting if there was an intent to run the
HOME: Starting: Intent { act=android.intent.action.MAIN
cat=[android.intent.category.HOME]
Is there any other way to do it or is it really impossible?
The optimal solution would have been to catch an intent in the Manifest.xml, like:
action android:name="android.intent.category.HOME"
and implement a new class to catch it. (But it doesn't seem to catch it).
I would like to know when the user pressed the home button while he was running my app.
What specifically are you trying to achieve?
BUT: The problem is that I don't want to edite the existing code.
By definition, that is impossible.
The only solution I found was to add a service to the application which listens to the Log detecting if there was an intent to run the
That is modifying the code, violating your own requirement. Moreover, it requires a permission that really you should not be asking for.
Is there any other way to do it or is it really impossible?
That depends on what specifically you are trying to achieve.
The optimal solution would have been to catch an intent in the Manifest.xml...and implement a new class to catch it. (But it doesn't seem to catch it).
HOME is a category. It is not an action. Home screens are activities that respond to the MAIN action in the HOME category. However, it is modifying the code, violating your own requirement.
You can use key press event handler
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
// Home key pressed
} else {
return super.onKeyDown(keyCode, event);
}
return false;
}

Notifications opening activities and the back button? Restricting to one instance?

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);
}

Categories

Resources