AndroidのActivity之后台隐藏
1 public class MainActivity extends Activity { 2 /** Called when the activity is first created. */ 3 NotificationManager nm; 4 Notification notifi; 5 PendingIntent pi; 6 @Override 7 public void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 10 notifi = new Notification(R.drawable.ic_launcher,"",System.currentTimeMillis()); 11 Intent intent = new Intent(this,MainActivity.class); 12 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); 13 pi = PendingIntent.getActivity(this, 0, intent, 0); 14 notifi.setLatestEventInfo(this,"别当心", "哥还在", pi); 15 notifi.flags = Notification.FLAG_INSISTENT|Notification.FLAG_ONGOING_EVENT; 16 setContentView(R.layout.main); 17 18 } 19 20 @Override 21 public void onBackPressed() { 22 // TODO Auto-generated method stub 23 //super.onBackPressed(); 24 AlertDialog.Builder builder = new AlertDialog.Builder(this); 25 builder.setIcon(R.drawable.ic_launcher); 26 builder.setTitle("是否退出"); 27 //builder.setMessage(""); 28 builder.setPositiveButton("退出", new DialogInterface.OnClickListener() { 29 30 @Override 31 public void onClick(DialogInterface dialog, int which) { 32 // TODO Auto-generated method stub 33 finish(); 34 android.os.Process.killProcess(android.os.Process.myPid()); 35 } 36 }); 37 builder.setNeutralButton("隐藏", new DialogInterface.OnClickListener() { 38 39 @Override 40 public void onClick(DialogInterface dialog, int which) { 41 // TODO Auto-generated method stub 42 Intent intent = new Intent(Intent.ACTION_MAIN); 43 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 44 intent.addCategory(Intent.CATEGORY_HOME); 45 MainActivity.this.startActivity(intent); 46 47 } 48 }); 49 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { 50 51 @Override 52 public void onClick(DialogInterface dialog, int which) { 53 // TODO Auto-generated method stub 54 55 } 56 }); 57 58 builder.create().show(); 59 } 60 61 62 63 64 @Override 65 public void finish() { 66 // TODO Auto-generated method stub 67 super.finish(); 68 nm.cancel(R.drawable.ic_launcher); 69 } 70 71 @Override 72 protected void onDestroy() { 73 // TODO Auto-generated method stub 74 super.onDestroy(); 75 Log.e("MainActivity", "onDestroy"); 76 nm.cancelAll(); 77 } 78
79 @Override 80 protected void onStop() { 81 // TODO Auto-generated method stub 82 super.onStop(); 83 nm.notify(R.drawable.ic_launcher, notifi); 84 } 85 86 87