像微信、QQ一样,按返回键只是切activity到后台

来源:互联网 发布:手机标尺软件 编辑:程序博客网 时间:2024/05/20 07:13

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

或者

PackageManager pm = getPackageManager();ResolveInfo homeInfo = pm.resolveActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), 0);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); void startActivitySafely(Intent intent) {   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   try {       startActivity(intent);   } catch (ActivityNotFoundException e) {       Toast.makeText(this, "unabletoopensoftware",Toast.LENGTH_SHORT).show();   } catch (SecurityException e) {       Toast.makeText(this, "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);   }}


两种方法。

0 0
原创粉丝点击