第一种方法

Intent intent = new Intent(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_HOME);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(intent);

第二种方法

此方法其实不是主要是屏蔽Keycode_Back,让它不结束(finish())Activity,直接显示HOME界面。

PackageManager pm = getPackageManager();                                ResolveInfo homeInfo = pm.resolveActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), 0);

public boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK) {ActivityInfo ai = homeInfo.activityInfo;Intent startIntent = new Intent(Intent.ACTION_MAIN);startIntent.addCategory(Intent.CATEGORY_LAUNCHER);startIntent.setComponent(new ComponentName(ai.packageName,ai.name));startActivitySafely(startIntent);return true;} elsereturn super.onKeyDown(keyCode, event);}

void startActivitySafely(Intent intent) {intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);try {startActivity(intent);} catch (ActivityNotFoundException e) {Toast.makeText(this, R.string.unabletoopensoftware,Toast.LENGTH_SHORT).show();} catch (SecurityException e) {Toast.makeText(this, R.string.unabletoopensoftware,Toast.LENGTH_SHORT).show();Log.e(TAG,"Launcher does not have the permission to launch "+ intent+ ". Make sure to create a MAIN intent-filter for the corresponding activity "+ "or use the exported attribute for this activity.",e);}}

更多相关文章

  1. [rk3399][Android(安卓)6.0]隐藏状态栏
  2. android webView判断是否加载完成的2种办法
  3. android 打开本地相册,并将选择的照片显示到当前界面
  4. Android之十二微信UI界面设计
  5. Android(安卓)之屏蔽,捕获Home键
  6. Android(安卓)接收短信并屏蔽notification 提示
  7. Android(安卓)Handler不同界面发送数据
  8. Android(安卓)用户界面【level 1】
  9. Android腾讯微薄客户端开发十二:博主的偶像

随机推荐

  1. html5实现点击弹出图片功能
  2. 10月09日作业 foreach多级循环写法
  3. 用foreach遍历省市区
  4. 遍历数组,输出省市区名称
  5. 数组遍历代码
  6. PHP 循环遍历数组
  7. 使用php数组遍历及模拟数据制作一个省级
  8. Foreach循环遍历数组
  9. PHP数据类型及开发环境部署
  10. 初始 php 及认识 php 变量与数据类型