一.Service的简介
1.Service介绍和作用
Service是Android系统中的四大组件之一,它是一种长生命周期的,没有可视化界面,运行于后台的一种服务程序。比如我们播放音乐的时候,有可能想边听音乐边干些其他事情,当退出播放音乐的应用,如果不用Service,我 们就听不到歌了,所以这时候就得用到Service了。
2、Service的俩种启动方式
1、通过Context.startService(intent)方法启动服务,这种方式启动服务,服务会一直在后台执行,除非用户在设置中把Service关闭,或者调用了StopService(intent)方法;多次执行启动服务的方法只会执行onCreate方法一次。会每次都执行onStartCommend()方法,根据此方法的返回值决定服务被杀死后是否自动启动。如下
- START_STICKY 粘性的 被杀死会自动启动 但是无Intent
- START_REDELIVER_INTENT 粘性的 会自动启动, 传递intent
- START_NOT_STICKY 非粘性的 不会自动启动
- 调用父类的Super.OnStartCommand()是粘性 无intent
生命周期执行的方法
onCreate()
onStartCommand()
Service running
onDestroy()方法
2、绑定方式启动服务(Context.bindService()/unBindService())
此方式启动服务,服务的生命周期与调用的客户端组件的生命周期有关,执行的方法如下
1、onCreate
2、onBind
Service running 如果绑定者被销毁,服务也会解绑,并执行断开连接的方法
3、onUnBind()(如果还有组件绑定着)则服务继续执行没有绑定者了
4、onDestroy()
3、混合时启动startService()—bindSevice()—–unBindService —stopService()
onCreate()—onStartCommand()–服务运行 -onBind() –onUnbind()—调用Stop—onDestroy();
代码如下
1、MyService类
package com.kuxiao.testservice;
import java.util.Random;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
// START_STICKY 粘性的 被杀死会自动启动 但是无Intent
// START_REDELIVER_INTENT 粘性的 会自动启动, 传递intent
// START_NOT_STICKY 非粘性的 不会自动启动
// 调用父类的Super.OnStartCommand()是粘性 无intent
@Override
public void onCreate() {
Log.d("MyService --->>", "MyService服务创建了...");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
String from = intent.getStringExtra("from");
Log.d("MyService --->>", "onStartCommand的方法...from:" + from);
}
return START_STICKY;
}
private MyBinder mMyBinder = new MyBinder();
private Random mMyRandom = new Random();
@Override
public boolean onUnbind(Intent intent) {
Log.d("MyService --->>", "unbindService方法....");
return super.onUnbind(intent);
}
@Override
public IBinder onBind(Intent intent) {
if (intent != null) {
String from = intent.getStringExtra("from");
Log.d("MyService --->>", "onBind方法....form :" + from);
}
return mMyBinder;
}
class MyBinder extends Binder {
public MyService getService() {
return MyService.this;
}
}
@Override
public void onDestroy() {
Log.d("MyService --->>", "MyService服务销毁了...。。。");
super.onDestroy();
}
public int getRandomCount() {
return mMyRandom.nextInt(1000);
}
}
2、MainActivity类
package com.kuxiao.testservice;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.kuxiao.testservice.MyService.MyBinder;
public class MainActivity extends ActionBarActivity implements OnClickListener {
private Button mBtn_startService;
private Button mBtn_stopService;
private Button mBtn_bindService;
private Button mBtn_unbindService;
private Button mBtn_startActivity;
private MyService mMyService = null;
private boolean isBind = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
isBind = true;
MyBinder myBinder = (MyBinder) service;
Log.d("MainActivity --->>", "MainActivity绑定service....");
mMyService = myBinder.getService();
mMyService.getRandomCount();
Log.d("MainActivity --->>",
"绑定service获取到的随机数为...." + mMyService.getRandomCount());
}
};
private void initView() {
mBtn_startService = (Button) findViewById(R.id.id_btn_startservice);
mBtn_stopService = (Button) findViewById(R.id.id_btn_stopservice);
mBtn_bindService = (Button) findViewById(R.id.id_btn_bindService);
mBtn_unbindService = (Button) findViewById(R.id.id_btn_unbindservice);
mBtn_startActivity = (Button) findViewById(R.id.id_btn_startAcitityB);
mBtn_startService.setOnClickListener(this);
mBtn_bindService.setOnClickListener(this);
mBtn_unbindService.setOnClickListener(this);
mBtn_startActivity.setOnClickListener(this);
mBtn_stopService.setOnClickListener(this);
Log.d("MainActivity --->>", "MainActivity创建了...OnCreate....");
}
@Override
protected void onStart() {
Log.d("MainActivity --->>", "MainActivity开始了...onStart....");
super.onStart();
}
@Override
protected void onResume() {
Log.d("MainActivity --->>", "MainActivity可交互了...onResume....");
super.onResume();
}
@Override
protected void onPause() {
Log.d("MainActivity --->>", "MainActivity可暂停了...onPause....");
super.onPause();
}
@Override
protected void onRestart() {
Log.d("MainActivity --->>", "MainActivity重新开始了...onRestart....");
super.onRestart();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.id_btn_startservice:
Intent intent = new Intent(this, MyService.class);
intent.putExtra("from", this.getClass().getSimpleName());
startService(intent);
break;
case R.id.id_btn_stopservice:
Intent intent1 = new Intent(this, MyService.class);
intent1.putExtra("from", this.getClass().getSimpleName());
stopService(intent1);
break;
case R.id.id_btn_bindService:
Intent intent2 = new Intent(this, MyService.class);
intent2.putExtra("from", this.getClass().getSimpleName());
bindService(intent2, conn, BIND_AUTO_CREATE);
break;
case R.id.id_btn_unbindservice:
if (isBind) {
isBind = false;
unbindService(conn);
Log.d("MainActivity --->>", "MainActivity解除绑定service....");
}
break;
case R.id.id_btn_startAcitityB:
Intent intent3 = new Intent(this, MainActivity1.class);
startActivity(intent3);
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
3、MainActivity1类
package com.kuxiao.testservice;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.kuxiao.testservice.MyService.MyBinder;
public class MainActivity1 extends ActionBarActivity implements OnClickListener {
private Button mBtn_bindService;
private Button mBtn_unbindService;
private Button mBtn_startActivity;
private MyService mMyService = null;
private boolean isBind = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
initView();
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
isBind = true;
MyBinder myBinder = (MyBinder) service;
Log.d("MainActivity1 --->>", "MainActivity 1绑定service....");
mMyService = myBinder.getService();
mMyService.getRandomCount();
Log.d("MainActivity1 --->>",
"绑定service获取到的随机数为...." + mMyService.getRandomCount());
}
};
private void initView() {
mBtn_bindService = (Button) findViewById(R.id.id_btn_bindService1);
mBtn_unbindService = (Button) findViewById(R.id.id_btn_unbindservice1);
mBtn_startActivity = (Button) findViewById(R.id.id_btn_finish);
mBtn_bindService.setOnClickListener(this);
mBtn_unbindService.setOnClickListener(this);
mBtn_startActivity.setOnClickListener(this);
Log.d("MainActivity1 --->>", "MainActivity1创建了...OnCreate....");
}
@Override
protected void onStart() {
Log.d("MainActivity1 --->>", "MainActivity1开始了...onStart....");
super.onStart();
}
@Override
protected void onResume() {
Log.d("MainActivity1 --->>", "MainActivity1可交互了...onResume....");
super.onResume();
}
@Override
protected void onPause() {
Log.d("MainActivity1 --->>", "MainActivity1可暂停了...onPause....");
super.onPause();
}
@Override
protected void onRestart() {
Log.d("MainActivity1 --->>", "MainActivity1重新开始了...onRestart....");
super.onRestart();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.id_btn_bindService1:
Intent intent2 = new Intent(this, MyService.class);
intent2.putExtra("from", this.getClass().getSimpleName());
bindService(intent2, conn, BIND_AUTO_CREATE);
break;
case R.id.id_btn_unbindservice1:
if (isBind) {
isBind = false;
unbindService(conn);
Log.d("MainActivity1 --->>", "MainActivity1解除绑定service....");
}
break;
case R.id.id_btn_finish:
finish();
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}