Home > Uncategorized > How to Control/Disable Home Button

How to Control/Disable Home Button

All Technical Solution – Disable Home Button in Android

You can’t control the home button as now, You can not disable the home button, you need to write your own firmware bla..bla..

But you can control you Home Button, disable you Home button.


<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.ssc.save.instance”
android:versionCode=”1″
android:versionName=”1.0″>
<application android:icon=”@drawable/icon” android:label=”@string/app_name”>
<activity android:name=”.SavedInstace”
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.MONKEY”/>
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>

<activity android:name=”Activity2″></activity>

</application>
<uses-permission android:name=”android.permission.SET_PREFERRED_APPLICATIONS”/>

<uses-sdk android:minSdkVersion=”5″ />

</manifest>
package com.ssc.save.instance;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/*
* We have two activity Activity1 and Activity2
* In Activity1, have two EditText field and a Save button
* clicking on Save button, Activity2 get called and click of submit Button of Activity2,
* Activity1 get called.
* Now I want to preserve the Activity1 data, while moving to Activity2, and vice versa
*/
public class SavedInstace extends Activity {
/** Called when the activity is first created. */
Button submit;
EditText et1;
EditText et2;
Bundle savedInstanceState;
private static final String TAG = “SavedInstace”;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// this.savedInstanceState = savedInstanceState;
submit = (Button) findViewById(R.id.submit);
et1 = (EditText) findViewById(R.id.et_one);
et2 = (EditText) findViewById(R.id.et_two);
String one = getIntent().getStringExtra(“one”);
String two = getIntent().getStringExtra(“two”);
if ((one == null) || (two == null)) {
// do nothing
} else {
one = getIntent().getStringExtra(“one”);
two = getIntent().getStringExtra(“two”);
et1 = (EditText) findViewById(R.id.et_one);
et2 = (EditText) findViewById(R.id.et_two);
et1.setText(one);
et2.setText(two);
}
// if (savedInstanceState != null) {
// String one = null;
// String two = null;
// one = savedInstanceState.getString(“one”);
// two = savedInstanceState.getString(“two”);
// et1 = (EditText) findViewById(R.id.et_one);
// et2 = (EditText) findViewById(R.id.et_two);
// et1.setText(one);
// et2.setText(two);
// }
// Set as home activity
// This is done so we can appear to disable the Home key.
PackageManager pm = getPackageManager();
//pm.addPackageToPreferred(getPackageName());

IntentFilter filter = new IntentFilter(“android.intent.action.MAIN”);
filter.addCategory(“android.intent.category.HOME”);
filter.addCategory(“android.intent.category.DEFAULT”);

ComponentName[] components = new ComponentName[]
{
new ComponentName(“com.android.launcher”, “.Launcher”)
};

Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(),
SavedInstace.class.getName());

pm.clearPackagePreferredActivities(“com.android.launcher”);
pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY,
components, component);

submit.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent activity2 = new Intent(SavedInstace.this,
Activity2.class);
String one = et1.getText().toString();
String two = et2.getText().toString();
activity2.putExtra(“one”, one);
activity2.putExtra(“two”, two);
startActivityForResult(activity2, 0);
}
});
}

@Override
protected void onPause() {
Log.d(TAG, “onPause”);
super.onPause();
}

@Override
protected void onResume() {
Log.d(TAG, “onResume”);
super.onResume();

}

@Override
protected void onRestart() {
Log.d(TAG, “onRestart”);
super.onRestart();
// onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
Log.d(TAG, “onSaveInstanceState”);
String one = null;
String two = null;
boolean b = false;
et1 = (EditText) findViewById(R.id.et_one);
et2 = (EditText) findViewById(R.id.et_two);
one = et1.getText().toString();
two = et2.getText().toString();

outState.putString(“one”, one);
outState.putString(“two”, two);
super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.d(TAG, “onRestoreInstanceState”);
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null) {
String one = null;
String two = null;
one = savedInstanceState.getString(“one”);
two = savedInstanceState.getString(“two”);
et1 = (EditText) findViewById(R.id.et_one);
et2 = (EditText) findViewById(R.id.et_two);
et1.setText(one);
et2.setText(two);
}

}

public class SavedInstaceHolder {

}
}

Advertisements
Categories: Uncategorized
  1. June 8, 2011 at 11:19 am

    This is horrible code. Do not request the HOME category unless *you are a home screen*. SDK applications cannot call clearPackagePreferredActivities() except for their own package. onSaveInstanceState() does not need to manually handle EditText widgets, as the built-in implementation handles those for you. And so on.

  2. Gaurav
    July 30, 2011 at 8:15 am

    Thank Dude…….. Great job 🙂

  3. January 3, 2012 at 8:54 pm

    Thanks!, that was the answer to my question in stackoverflow, some people laugh of me, haha http://stackoverflow.com/questions/8715658/how-i-cant-avoid-both-the-app-close-and-the-go-in-background/8716135#8716135

    maybe you can use some code plugin 😉 in this post

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: