第一种方法
1
Intent intent = new Intent(Intent.ACTION_MAIN);
2
intent.addCategory(Intent.CATEGORY_HOME);
3
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
4
startActivity(intent);
第二种方法
此方法其实不是主要是屏蔽Keycode_Back,让它不结束(finish())Activity,直接显示HOME界面。
1
PackageManager pm = getPackageManager();
2
ResolveInfo homeInfo = pm.resolveActivity(new Intent(Intent.ACTION_MAIN)
3
.addCategory(Intent.CATEGORY_HOME), 0);
01
public boolean onKeyDown(int keyCode, KeyEvent event) {
02
if (keyCode == KeyEvent.KEYCODE_BACK) {
03
ActivityInfo ai = homeInfo.activityInfo;
04
Intent startIntent = new Intent(Intent.ACTION_MAIN);
05
startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
06
startIntent.setComponent(new ComponentName(ai.packageName,
07
ai.name));
08
startActivitySafely(startIntent);
09
return true;
10
} else
11
return super.onKeyDown(keyCode, event);
12
}
01
void startActivitySafely(Intent intent) {
02
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
03
try {
04
startActivity(intent);
05
} catch (ActivityNotFoundException e) {
06
Toast.makeText(this, R.string.unabletoopensoftware,
07
Toast.LENGTH_SHORT).show();
08
} catch (SecurityException e) {
09
Toast.makeText(this, R.string.unabletoopensoftware,
10
Toast.LENGTH_SHORT).show();
11
Log
12
.e(
13
TAG,
14
Launcher does not have the permission to launch
15
+ intent
16
+. Make sure to create a MAIN intent-filterfor the corresponding activity
17
+or use the exported attribute for thisactivity. ,
18
e);
19
}
20
}