进程优先级:
1.Foreground Process 前台进程
2.Visible Process可视进程
3.Service Process 服务进程
4.Background Process 后台进程 LRU
5.Empty Process 空进程
package com.example.startservice;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View v){
//使用显示意图开启Service
Intent service = new Intent(this,MyServic.class);
//通过方法开启一个服务
startService(service);
}
public void stop(View v){
Intent name = new Intent(this,MyServic.class);
//用startService方法开启的服务,要用stopService停止
stopService(name);
}
}
package com.example.startservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyServic extends Service{//Service没有界面的Activity
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.e("TAG", "onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("TAG", "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("TAG", "onDestroy");
}
}
</activity>
<service android:name=".MyService">
</service>