基本原理是:在运行或者显示Activity时,如果需要隐藏时,将Activity跳转到Home主页面就可以,就可以"虚假的"隐藏了Activity了,下面是Demo:
package com.example.androidhideactivity; import android.os.Bundle; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.ComponentName; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private final static String TAG="MainActivity"; private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton=(Button)findViewById(R.id.button1); mButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub //隐藏activity到后台,打开HOME Launcher 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)); startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { startActivity(startIntent); } catch (ActivityNotFoundException e) { Log.i(TAG,"not found Activity error="+e.getMessage()); } catch (SecurityException e) { Log.i(TAG,"not found Activity error="+e.getMessage()); Log.e(TAG,"Launcher does not have the permission to launch "+ startIntent + ". Make sure to create a MAIN intent-filter for the corresponding activity "+ "or use the exported attribute for this activity.", e); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }