什么是Service
1.Service是Android四大组件之一,和Activity的级别相当。
2.Service是可以长时间,运行在后台的,是不可见,是没有界面的组件。
3.Service是运行在主线程中的
4.Service可以跨进程调用
为什么要学习Service
1.大部分病毒利用service的特点,可以在不知不觉中完成预设的功能
2.作为安卓工程师,至少需要了解service是如何在后台完成预设功能
3.优先级比较高
4.在某个功能需要执行很长时间,并且在这个执行过程中,不需要让用户操作,不需要在Activity进行交互的情况下,可以通过Service在后台完成指定任务
Service有哪些应用场景
1.要在后台2.耗时比较久
如何使用Serivce
用startservice启动service
1:新建类继承Service
public class MyService extends Service
2:重写onCreate方法
public void onCreate() {
super.onCreate();
}
3:实现onBind方法
public IBinder onBind(Intent intent)
4:onStartCommand方法
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() {
@Override
public void run() {
while (count<100){
Log.e("onStartCommand", "run: "+count );
count++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
5:onDestroy方法
6:在AndroidManifest文件中注册Service
<service android:name="com.lenovo.mymediaplayer.service.MusicService" >
</service>
7:通过startService启动Service
Intent intent = new Intent(MyServiceActivity.this, MyService.class);
startService(intent);
startServince生命周期:
context.startService()–>onCreate()–>onStartCommand()–>context.stopService()–>onDestory()–>Service stop
用bindServince绑定Servince
前三步和上面差不多
1.新建类继承Service
2.重写onCreate方法
3.实现onBind抽象方法
public class myBinder extends Binder {
BindService getService() {
return BindService.this;
}
}
实例化MyBind
private final IBinder binder = new MyBinder();
实现onBind方法
public IBinder onBind(Intent intent) {
return binder;
}
重写onUnBind方法 在调用unbindService方法解除绑定后,触发此回调函数
重写onDestory方法
在AndroidManifest中注册
实现ServiceConnection接口
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
bindService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
bindService = ((BindService.MyBinder) service).getService();
System.out.println("Service连接成功");
bindService.excute();
}
};
通过bindService绑定Service
Intent intent = new Intent(this, BindService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
通过unbindService解绑Service
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
bindServince生命周期
context.startService()–>onCreate()–>onBind()–>Service running()–>onUnbind()–>onDestory()–>Service stop
IntentServince有什么不同
bindService,startService都是运行在主线程中,如果有耗时操作,需要我们自己开线程并维护线程,IntentService不需要开启子线程
IntentServince怎么用,注意事项
新建一个类继承intentServince
启动Service只能用start启动Service
Activity完整代码:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.myapplication.R;
public class MyIntentServiceActivity extends AppCompatActivity implements View.OnClickListener{
private Button myIntentservice_start, myIntentservice_stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_intent_service);
myIntentservice_start = findViewById(R.id.myIntentservice_start);
myIntentservice_start.setOnClickListener(this);
myIntentservice_stop = findViewById(R.id.myIntentservice_stop);
myIntentservice_stop.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.myIntentservice_start:
Intent intent = new Intent(MyIntentServiceActivity.this, MyIntentService.class);
startService(intent);//启动service
break;
case R.id.myIntentservice_stop:
Intent intent2 = new Intent(MyIntentServiceActivity.this, MyIntentService.class);
stopService(intent2);//停止service
break;
default:
break;
}
}
}