Overriding the Home Button - How to Get Rid of the Choice

Overriding the Home button - how do I get rid of the choice?

You can't permanently override the Home button without the user confirming it.

One argument for why this is the case is a security one. The Home button is the one way a user can be guaranteed to exit any application. If you could make the Home button launch your application instead of the Home screen without the user confirming this change, it would then be very easy to write a malicious application that hijacked a user's phone.

Alternatively, your application could contain a replicate Home Screen that harvested a user's Google account details; not that hard since the source is available. If your application could silently replace the default Home Screen, it would be hard for the user to tell this had happened.

Also, do you really want to override Home? Home is like an emergency escape button. Pressing Home twice will always take a user back to the center panel of the Home Screen, so whatever application they're running, it's easy for a user to get back to somewhere they know. You shouldn't really be overriding this unless you're producing a full Home replacement.

How can I enable or disable the home button on Android?

By implementing Overided methods onAttachedToWindow() and onKeyDown() it's work fine.

@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
MainActivity.this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_HOME) {
Log.d("Home Button", "Clicked");
}
if (keyCode == KeyEvent.KEYCODE_BACK) {

finish();
}
return false;
};

Without overriding onAttachedToWindow method KEYCODE_HOME doesn't work.

Note:Home key press is handled by the framework and is never delivered to applications.

This is a flaw in version <4.0 and is not working from ICS.

Override home button behaviour

you cant make it work as you want, but you can disable it

@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}

and you can tell user that home button is disabled:

public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
Context context = getApplicationContext();
Toast toast = Toast.makeText(context,"Home button is disabled",1);
toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
toast.show();
return true;
}
return super.onKeyDown(keyCode, event);
}

Override home and back button is case a boolean is true

I was wondering if I can override the action of the back and home button is some cases.

Yes you can do override Home button.

I have developed an application which disable hard button, you can have a look.
I have taken a toggle button which locks all hard button to work except Power button

public class DisableHardButton extends Activity {
/** Called when the activity is first created. */
TextView mTextView;
ToggleButton mToggleButton;
boolean isLock=false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView=(TextView) findViewById(R.id.tvInfo);
mToggleButton=(ToggleButton) findViewById(R.id.btnLock);


mToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
isLock=isChecked;
onAttachedToWindow();
}
});
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {

if ( (event.getKeyCode() == KeyEvent.KEYCODE_HOME) && isLock) {
mTextView.setText("KEYCODE_HOME");
return true;
}
else
return super.dispatchKeyEvent(event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub

if( (keyCode==KeyEvent.KEYCODE_BACK) && isLock)
{
mTextView.setText("KEYCODE_BACK");
return true;
}
else
return super.onKeyDown(keyCode, event);
}
@Override
public void onAttachedToWindow()
{
System.out.println("Onactivity attached :"+isLock);
if(isLock)
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
else
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION);
super.onAttachedToWindow();
}
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/tvInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<ToggleButton
android:id="@+id/btnLock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="UnLocked"
android:textOn="Locked" />

</LinearLayout>

Close current activity on android Device's Home button click

I have found the way for this is like,

when user press home button, current activity's pause method called.

after a second of activity goes in background , if no other activity is opened(i have used global boolean variable), then I broadcast the message to all activity to be closed.

So whichever activity is open, will be closed on that receiver.

It works for me. Hope so it could help you as well.



Related Topics



Leave a reply



Submit