I would like to know exactly what operation a devices home button performs? ie what intent, intent category and action is issued when you click on home button? that takes on back to the blank home screen. I would like to know what is involved in implementing this operation to occur when clicking on my own custom button. Thanks (PS I know it is not standard, but neither is my device).
Asked
Active
Viewed 1.2k times
4 Answers
14
If you want to show the home screen, you can do it by:
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
Update: check this sample app: http://developer.android.com/resources/samples/Home/index.html

Aleadam
- 40,203
- 9
- 86
- 108
-
What effect will this have on the application that issues it? It will go to background? – Androider Apr 02 '11 at 02:53
-
Yes. It does not close the app, it only brings the home screen to the foreground. – Aleadam Apr 02 '11 at 03:12
-
Is that the same effect however of clicking the home button? – Androider Apr 02 '11 at 03:56
-
To the best of my knowledge, yes. Bear in mind that any app can capture the keydown event and do whatever the app wants with it before the event gets passed (or not) to the framework (services.jar, if I remember correctly) – Aleadam Apr 02 '11 at 04:00
-
@Aleadam "It does not close the app" there's no such thing like "close the app" in Android. – Marian Paździoch Jul 17 '15 at 14:23
0
This is the intent in xml, in case you are looking for it:
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<!-- The following two intent-filters are the key to set homescreen -->
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Oded Breiner
- 28,523
- 10
- 105
- 71
-1
You can refer to the code:
Button btnHome;
btnHome = (Button) findViewById(R.id.Home);
btnHome.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// sendKey(KeyEvent.KEYCODE_HOME);
myHandler.sendEmptyMessage(Home);
}
});
class myRunnable implements Runnable {
public myRunnable(int key) {
this.keycode = key;
}
int keycode;
@Override
public void run() {
sendKey(keycode);
}
public void sendKey(int keyCode) {
System.out.println("Judy--------------------->sendkey " + keyCode);
long now = SystemClock.uptimeMillis();
long n = System.currentTimeMillis();
try {
KeyEvent down = new KeyEvent(now, now, KeyEvent.ACTION_DOWN,
keyCode, 0);
KeyEvent up = new KeyEvent(now, now, KeyEvent.ACTION_UP,keyCode, 0);
IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager
.getService("window"));
wm.injectKeyEvent(down, false);
wm.injectKeyEvent(up, false);
} catch (RemoteException e) {}
}
}

Mr_and_Mrs_D
- 32,208
- 39
- 178
- 361

Judy
- 1,772
- 6
- 27
- 48
-
-
-
really ? can you please explain your code ? there is no definition of myHandler in your code - please edit your answer explaining how it answers the question and let me know – Mr_and_Mrs_D Jul 18 '13 at 15:01