activate an application when a power button is clicked - android

i want to create such application in android which will be activated when i press sleep or power button twice , is it possible to do that , by running an application in background and listening events from power button ?
some times phone gets into sleep mode once it is idle , and to use any application user has to press
sleep button and then he has to enter certain password to activate the phone. But i want to make make it activate my application when a power button is clicked without any other intervention

You can try this trick .
Register a Broadcast Receiver which is initiated when powerbutton is clicked.
Now in OnReceive method of the Receiver do what you want.
For example:
in manifest file register a receiver:
<receiver android:name="com.test.check.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"></action>
<action android:name="android.intent.action.SCREEN_ON"></action>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
<action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
</intent-filter>
</receiver>
&& in onReceive() method of the Receiver
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.v("##%#%#", "Power button is pressed.");
Toast.makeText(arg0, "power button clicked",Toast.LENGTH_LONG).show();
//perform what you want here
}
}
Now perform any operation in onReceive() method of the Receiver.

this is an key down event of power you can get some idea from this just try on this you'll get some idea...
i didnt try such an act but i found this in power manager.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
// do what you want with the power button
return true;
}
return super.onKeyDown(keyCode, event);
}

Related

How to detect key events in service and broadcast receiver

How can I get key events when a user presses various buttons in a service or broadcast receiver? I'm specifically interested in knowing when the user presses a volume button so that I can trigger something else in the background, like a voice recorder.
Unfortunately, my internet searches haven't produced any results.
Something like the following should work:
From official documentation:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"...>
//code, like activities, etc
<receiver android:name="com.example.test.VolumeBroadcast" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</application>
Example of a receiver:
public class VolumeBroadcast extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
//check the intent something like:
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) {
// Handle key press.
}
}
}
}
The way you register is like that:
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
// Start listening for button presses
am.registerMediaButtonEventReceiver(RemoteControlReceiver);
// Stop listening for button presses
am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
Page below:
Audio Playback

Override home Button in Android

I need to override Home button for Lock Screen App. And I found the following answer which restart the app after 6 seconds on home button press.
protected void onUserLeaveHint() {
Intent i=new Intent(this,MainActivity123.class);
startActivity(i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
Toast.makeText(this,"leaveHint",Toast.LENGTH_SHORT).show();
}
But I want to start the app immediately. So I guess thier was an answer saying if we make our app default Launcher app than thier will be no time gap.So i added in my manifest
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
But this is not working and app still restart after 6 sec.
what should i do.
So Spend some more researching and found another method {used to go to a default screen in launcher When you are already on home screen}
The method called automatically using appropriate flag in startActivity()
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Toast.makeText(this,"onNewEvent",Toast.LENGTH_SHORT).show();
if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Toast.makeText(this,"onNewEvent",Toast.LENGTH_SHORT).show();
//++goHome++
startActivity(intent);
}
}
The method is get called But app still starts after 6 sec. SO no progress Any suggestion........
Try overriding the home button by implementing this method:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
Toast.makeText(this, "You pressed the home button!", Toast.LENGTH_LONG).show();
return true;
}
return super.onKeyDown(keyCode, event);
}
:) Hope it helps.

How to launch particular application when Power Button is pressed twice in Android [duplicate]

i want to create such application in android which will be activated when i press sleep or power button twice , is it possible to do that , by running an application in background and listening events from power button ?
some times phone gets into sleep mode once it is idle , and to use any application user has to press
sleep button and then he has to enter certain password to activate the phone. But i want to make make it activate my application when a power button is clicked without any other intervention
You can try this trick .
Register a Broadcast Receiver which is initiated when powerbutton is clicked.
Now in OnReceive method of the Receiver do what you want.
For example:
in manifest file register a receiver:
<receiver android:name="com.test.check.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"></action>
<action android:name="android.intent.action.SCREEN_ON"></action>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
<action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
</intent-filter>
</receiver>
&& in onReceive() method of the Receiver
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.v("##%#%#", "Power button is pressed.");
Toast.makeText(arg0, "power button clicked",Toast.LENGTH_LONG).show();
//perform what you want here
}
}
Now perform any operation in onReceive() method of the Receiver.
this is an key down event of power you can get some idea from this just try on this you'll get some idea...
i didnt try such an act but i found this in power manager.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
// do what you want with the power button
return true;
}
return super.onKeyDown(keyCode, event);
}

How to know if the user has pressed power key twice or thrice continuously outside the activity in android?

I need to know when the user will press the power key continuously 2 or 3 times.
But in the case the user is out of the application lets say on the home screen or even using any other application.
I getting the listener event of the power key on the activity but not on the service.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(KeyEvent.KEYCODE_POWER == event.getKeyCode()){
Log.e("POWER", "pow");
return true;//If event is handled, falseif
}
return super.onKeyDown(keyCode, event);
}
How can i know if the user has pressed the power button outside the activity?
You cannot override the power button press from your application. But when you press power button the screen turns on/off. So you can detect this using a broadcast receiver
<receiver android:name=".MyBroadCastReciever">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
</receiver>
and a broadcast receiver class
public class MyBroadCastReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//Take count of the screen off position
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
//Take count of the screen on position
}
}
}
Hope this is helpful for you.

MEDIA_BUTTON button event ACTION_DOWN and ACTION_UP received at the same time

In my app I want to measure how long the media button press was. I registered a broadcastReceiver that listens to the media button press: (please excuse stupid mistakes as I am a beginner...)
<receiver android:name="MyRec">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON">
<action android:name="android.intent.action.MEDIA_BUTTON"/>
</action>
</intent-filter>
</receiver>
The broadcastReceiver activates a method in an activity (not ideal, but this is just for testing purposes):
public void onReceive(Context context, Intent intent) {
KeyEvent Xevent = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
MainActivity inst = MainActivity.instance();
if ((Xevent.getAction() == KeyEvent.ACTION_DOWN)){
inst.startTimer();
}
else if ((Xevent.getAction() == KeyEvent.ACTION_UP)) {
inst.stopTimer();
}
}
The activity takes system time when startTimer() is called and then again when stopTimer is called and shows the diff:
public void startTimer(){
pressTime = System.currentTimeMillis();
}
public void stopTimer(){
pressDuration = System.currentTimeMillis() - pressTime;
TextView timerTextView = (TextView)findViewById(R.id.timerText);
timerTextView.setText(Long.toString(pressDuration));
}
The issue is that from what I see both events are called at the same time, both when I let go of the button, what eventually makes the timer count a very short time period (few millisecond) that are not related to the duration I press the button.
What am I doing wrong?
You don't need to use your own timers. You can use the getDownTime and getEventTime methods of the event parameter when receiving the KeyEvent.ACTION_UP action.
Also, The nested <action android:name="android.intent.action.MEDIA_BUTTON"/> in your manifest is not required

Categories

Resources