第一种方法
view source
print?
1 | Intent intent = new Intent(Intent.ACTION_MAIN); |
2 | intent.addCategory(Intent.CATEGORY_HOME); |
3 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
第二种方法
此方法其实不是主要是屏蔽Keycode_Back,让它不结束(finish())Activity,直接显示HOME界面。
view source
print?
1 | PackageManager pm = getPackageManager(); |
2 | ResolveInfo homeInfo = pm.resolveActivity(new Intent(Intent.ACTION_MAIN) |
3 | .addCategory(Intent.CATEGORY_HOME), 0); |
view source
print?
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, |
08 | startActivitySafely(startIntent); |
11 | return super.onKeyDown(keyCode, event); |
view source
print?
01 | void startActivitySafely(Intent intent) { |
02 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
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(); |
14 | ”Launcher does not have the permission to launch ” |
16 | + ”. Make sure to create a MAIN intent-filter for the corresponding activity ” |
17 | + ”or use the exported attribute for this activity.”, |
|