1: 首先第一点Service 不是子线程,这个一定要明确的,这个service是在后台运行,这里所谓的后台其实指的是没有界面而已
2: Service 分文Service 和IntentService(自带子线程)
3: Service的启动方式包含startService(和startActivity很类似) 和bindService,
通过startService启动的Service被删除的方法有哪两种?stopSelf() stopService
4: Service 和Messager的关系
5: onBind 方法:如果其他应用程序组件和服务进行绑定的时候,会自动回调这个方法, 返回null 或者是抛出异常不允许绑定服务
onCreate方法:当服务第一次启动之前,有系统来创建服务,并且调用这个方法,不管启动多少次,都只运行一次
onStartCommand方法:在每次执行startService的时候,都会调用,那么bindService的时候呢?应该是不调用的
onStartCommand方法是由主线程调用的,千万不要执行耗时的操作,可以在子线程中执行耗时操作
打印出当前的Thread.currentThread().toString(). 就会发现,的确是主线程调用的,所以千万不要执行耗时操作。
kodulf.servicedemo D/kodulf: onStartCommand: GOODThread[main,5,main]
kodulf.servicedemo D/kodulf: startServiceOne: Thread[main,5,main]
返回值,代表服务能否复活:
IntentService的onHandleIntent(Intent intent)
IntentService 通过打印log,我们可以发现的确是运行在一个新的子线程中的
2019-03-11 08:25:43.854 31734-31734/kodulf.servicedemo D/kodulf: startIntentService Thread[main,5,main]
2019-03-11 08:25:43.929 31734-31817/kodulf.servicedemo D/kodulf: onHandleIntent: www.baidu.comThread[IntentService[test],5,main]
onDestory 方法: 一定要在这个方法里面进行资源的释放,例如播放音乐的话,就需要释放掉音乐的资源
onDestory 的方法会在3个情况下调用:应用被强制关闭,stopSelf(), stopService,应该还有其他的情况
++++++++++++++++++++++++++++++++++++++
删除了进程什么意思?就是调用stopSelf()和stopService()来删除啊!
如果删除了进程后,会报错。 这个时候优化: 添加if(intent!=null){ return START_REDELIVER_INTENTif(intent!=null){String action = intent.getStringExtra("action");if(action!=null){if(action.equals("play")){//代表播放player.start();}else if(action.equals("pause")){//代表暂停player.pause();}}}return START_REDELIVER_INTENT;
package kodulf.servicedemo.services;import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;import kodulf.servicedemo.R;public class ServiceStandar extends Service {private String TAG = "kodulf";private MediaPlayer mMediaPlayer;public ServiceStandar() {}/*** 如果其他的应用程序组件和服务进行绑定过的时候,会自动调用这个方法* @param intent* @return*/@Overridepublic IBinder onBind(Intent intent) {// TODO: Return the communication channel to the service.throw new UnsupportedOperationException("Not yet implemented");}@Overridepublic void onCreate() {super.onCreate();mMediaPlayer = MediaPlayer.create(this,R.raw.threed);mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {@Overridepublic void onCompletion(MediaPlayer mp) {Log.d(TAG, "onCompletion: music");}});Log.d(TAG, "onCreate: ");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d(TAG, "onStartCommand: ");String test = intent.getStringExtra("Action");Log.d(TAG, "onStartCommand: " + test +Thread.currentThread().toString());
// return super.onStartCommand(intent, flags, startId);if("start".equals(test)){mMediaPlayer.start();}else if("stop".equals(test)){mMediaPlayer.pause();}else if("stopService".equals(test)){stopSelf();}//失忆后找回记忆return Service.START_STICKY;}@Overridepublic void onDestroy() {super.onDestroy();Log.d(TAG, "onDestroy: ");if(mMediaPlayer!=null) {mMediaPlayer.pause();mMediaPlayer.stop();//停止播放mMediaPlayer.release();//释放资源}}
}
package kodulf.servicedemo;import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;import kodulf.servicedemo.services.ServiceStandar;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void startServiceOne(View view) {Intent intent = new Intent(this,ServiceStandar.class);intent.putExtra("Action","start");Log.d("kodulf", "startServiceOne: " +Thread.currentThread().toString());startService(intent);}public void stopMusic(View view) {Intent intent = new Intent(this,ServiceStandar.class);intent.putExtra("Action","stop");Log.d("kodulf", "stopMusic: " +Thread.currentThread().toString());startService(intent);}public void stopService(View view) {Intent intent = new Intent(this,ServiceStandar.class);stopService(intent);}public void stopServiceUsingStopSelf(View view) {Intent intent = new Intent(this,ServiceStandar.class);intent.putExtra("Action","stopService");Log.d("kodulf", "stopService " +Thread.currentThread().toString());stopService(intent);}
}
Messager: