1.创建一个服务,让其继承自Serivce,并重写onBind方法,服务的创建是通过onCreate()方法实现,启动通过onStartCommand()方法,停止服务通过onDestory()方法。
2.没创建一个服务都会调用onCreate(),onStartCommand()方法,但是当一直启动服务,并且次数增加时,就会只调用onStartCommand()方法了,服务的停止调用onDestory()方法,停止后启动服务仍会调用onCreate(),onStartCommand()方法。
下面通过打印日志查看服务的创建启动停止过程。
package com.example.apple.service.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private final String TAG="MyService";
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG,"onCreate myService");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG,"onStartCommand myService");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy myService");
}
}
MainActivity.class:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_start;
private Button btn_stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_start=(Button)findViewById(R.id.start_service);
btn_stop=(Button)findViewById(R.id.stop_service);
btn_start.setOnClickListener(this);
btn_stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.start_service:
startService(new Intent(this, MyService.class));
break;
case R.id.stop_service:
stopService(new Intent(this,MyService.class));
break;
}
}
}
效果图:
Logcat打印日志:
06-27 09:08:04.023 2544-2544/com.example.apple.service D/MyService: onCreate myService
06-27 09:08:04.025 2544-2544/com.example.apple.service D/MyService: onStartCommand myService
06-27 09:08:07.586 2544-2544/com.example.apple.service D/MyService: onDestroy myService
06-27 09:08:09.821 2544-2544/com.example.apple.service D/MyService: onCreate myService
06-27 09:08:09.822 2544-2544/com.example.apple.service D/MyService: onStartCommand myService
06-27 09:08:11.246 2544-2544/com.example.apple.service D/MyService: onDestroy myService
06-27 09:08:13.132 2544-2544/com.example.apple.service D/MyService: onCreate myService
06-27 09:08:13.137 2544-2544/com.example.apple.service D/MyService: onStartCommand myService
06-27 09:08:14.673 2544-2544/com.example.apple.service D/MyService: onStartCommand myService
06-27 09:08:16.858 2544-2544/com.example.apple.service D/MyService: onStartCommand myService
06-27 09:08:17.703 2544-2544/com.example.apple.service D/MyService: onStartCommand myService
06-27 09:08:18.460 2544-2544/com.example.apple.service D/MyService: onStartCommand myService
06-27 09:08:23.127 2544-2544/com.example.apple.service D/MyService: onDestroy myService
06-27 09:08:25.128 2544-2544/com.example.apple.service D/MyService: onCreate myService
06-27 09:08:25.140 2544-2544/com.example.apple.service D/MyService: onStartCommand myService