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.
Related
I have an app that uses intent-filters in the AndroidManifest.xml file to start an Activity when the Home button is pressed. Thing is, if my app is on that time in a different activity, the LoginActivity is started again and the user has to log in again. I want the Home button to do nothing if my app is on top.
How could I do that?
<activity
android:name="com.example.LoginActivity"
android:label="#string/app_name" >
<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>
</activity>
Is not advisable to change the Home button functionality, because doing so it would be a negative user experience. Instead, you could store into a variable a boolean if the user is logged in, and if true, jump directly on to the main activity.
I don't know what is your launch activity, but if it is the login activity, you could check on the onStart method what I said, and then start the other activity.
You shoud to check by your own trigger (static variable?) that your app is working and user logged in early, then start another activity and finish current
OR
try this
android:launchMode="singleTop"
in your manifest to the activities.
There are three ways to do this:
Works between 2.3 and 4.0:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
// do nothing
return true;
}
return super.onKeyDown(keyCode, event);
}
Also works between 2.3 and 4.0:
#Override public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
This involves overriding the method onUserLeaveHint():
#Override
protected void onUserLeaveHint() {
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
((AlarmManager) this.getSystemService(ALARM)).set(1,
System.currentTimeMillis(),
PendingIntent.getActivity(this, 0, intent, 0));
}
super.onUserLeaveHint();
}
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);
}
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.
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
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);
}