Android:进程(process)优先级、startForeground
进程(process)的优先级(从高到低): 1. 前台进程 Foreground process 1) 当前用户操作的Activity所在进程 2) 绑定了当前用户操作的Activity的Service所在进程 3) 调用了startForeground()的Service 典型场景:后台播放音乐 <1>提高Service优先级的方法: 在Service中调用startFroeground(int id, Notification notify) <2>恢复Service优先级的方法: 在Service中调用stopFroeground(boolean removeNotify) 2. 可见进程Visible process 1) 处于暂停状态的Activity 2) 绑定到暂停状态的Activity的Service 3. 服务进程Service process 通过startService()启动的Service 4. 后台进程Background process 1) 处于停止状态的Activity 5. 空进程Empty process Activity端: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 public class MainActivity extends Activity implements OnClickListener { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button_startservice).setOnClickListener( this ); findViewById(R.id.button_stopservice).setOnClickListener( this ); findViewById(R.id.button_stopforeground).setOnClickListener( this ); textView =(TextView) findViewById(R.id.textView1); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true ; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_startservice: StartService(); break ; case R.id.button_stopservice: StopService(); break ; case R.id.button_stopforeground: StopForeground(); break ; default : break ; } } private void StopForeground() //恢复优先级,但不停止服务。oncreate只执行一次,但onstart每点一次就会执行一次 { Intent intent = new Intent( this , MyService. class ); intent.putExtra( "isStop" , true ); startService(intent ); } private void StopService() //停止服务 { Intent intent = new Intent( this , MyService. class ); stopService(intent ); } private void StartService() //启动服务,发出通知,设为前台优先级 { Intent service = new Intent( this , MyService. class ); startService(service ); } } Service端: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 public class MyService extends Service { @Override public void onCreate() { super .onCreate(); Notification notification = new Notification(R.drawable.ic_launcher, "启动服务发出通知" , System.currentTimeMillis()); //设置内容和点击事件 Intent intent = new Intent( this , MainActivity. class ); PendingIntent contentIntent = PendingIntent.getActivity( this , 0 ,intent , 0 ); notification.setLatestEventInfo( this , "优先级通知" , "提高优先级" , contentIntent); notification.flags =Notification.FLAG_AUTO_CANCEL; //设置为点击后自动取消 // NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // manager.notify(1235, notification); startForeground( 1235 , notification); //该方法已创建通知管理器,设置为前台优先级后,点击通知不再自动取消 } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent.getBooleanExtra( "isStop" , false )) //在onStart中降低优先级而不关闭服务 { Log.e( "Myservice" , "huifu()" ); huifu(); } return super .onStartCommand(intent, flags, startId); } private void huifu() //恢复优先级 { stopForeground( true ); } @Override public void onDestroy() { Log.e( "Myservice" , "onDestroy()" ); super .onDestroy(); } @Override public IBinder onBind(Intent intent) { throw new UnsupportedOperationException( "Not yet implemented" ); } } 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1205265,如需转载请自行联系原作者