1.什么是Service
1.Service是Android四大组件之一,和Activity的级别相当
2.Service是可以长时间运行在后台的,是不可见的,是没有界面的组件
3.Service是运行在主线程中的
4.Service可以跨进程调用、
2.Service有哪些应用场景
远程服务指的是服务和访问者不在同一个应用程序中,即不在同一个进程中。 访问远程服务类似进程间通信。 本地服务 启动的service是运行在主线程中的,所以耗时的操作还是要新建工作线程 用bindService时需要实现ServiceConnection
3.startService方式启动Service怎么做(启动和停止及生命周期)
使用startService方式启动Service的步骤:
1.新建类继承Service
2.重写onCreate方法:Service在创建时调用的方法,可以用来做数据初始化,Service在没有停止之前,只会执行一次
3.实现onBind抽象方法:由于Service是一个抽象类,定义了一个onBind抽象方法,所以必须实现此抽象方法,return null即可,此方法在startService方式用不上,无需理会即可
4.重写onStartCommand方法:Service创建后会调用onStartCommand此方法中可以接受调用者传递过来的参数,并且可以编写需要的逻辑代码,当重复调用Service时,onStartCommand会重复执行,onStart方法已弃用
5.重写onDestroy方法:在退出是销毁Service
6.在Manifest中注册Service
7.在有Context环境中通过startService启动Service
8.在有Context环境中通过stopService停止Service
具体代码如下:
@Override
public void onCreate() {
Log.e("TAG","onCreate*************************");
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e("TAG","onBind*************************");
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("TAG","onStartCommand*************************");
new Thread(new Runnable() {
@Override
public void run() {
int count=0;
while (count<100){
Log.e("TAG","run*************************"+count);
count++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.e("TAG","onDestroy*************************");
super.onDestroy();
}
Intent startintent=new Intent(this,Demo1Service.class);
startService(startintent);
Intent stopintent=new Intent(this,Demo1Service.class);
stopService(stopintent);
生命周期:
4.bindService方式启动Service怎么做(绑定和解绑及生命周期)
通过创建一个类继承Binder,相当将它当做一个连接器可以在另一个Activity中调用到Service,return当前Service的值,然后再Activity中通过创建一个ServiceConnection对象,再创建Demo1ServiceMyBinder对象,在通过myBinder.getService()得到,就能够调用到了Service中的方法了,再通过
bindService(startintent,serviceConnection,BIND_AUTO_CREATE);
unbindService(serviceConnection);
绑定和解绑就行了
代码如下:
public class MyBinder extends Binder{
public Demo1Service getService(){
return Demo1Service.this;
}
}
public void ff(){
new Thread(new Runnable() {
@Override
public void run() {
int i=0;
while (i<100){
i++;
Log.e("TAG","run----------------------"+i);
}
}
}).start();
}
private ServiceConnection serviceConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Demo1Service.MyBinder myBinder= (Demo1Service.MyBinder) service;
Demo1Service demo1Service=myBinder.getService();
demo1Service.ff();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
Intent startintent=new Intent(this,Demo1Service.class);
bindService(startintent,serviceConnection,BIND_AUTO_CREATE);
unbindService(serviceConnection);
与startService的启动和停止的方法略有不同
生命周期:
5.IntentService有什么不同
所有请求都在一个单线程中,不会阻塞应用程序的主线程,同一时间只处理一个请求。那么,用IntentService有什么好处呢?首先,我们省去了在Service中手动开线程的麻烦,第二,当操作完成时,我们不用手动停止Service,这也就是IntentService的不同
6.IntentService怎么用
// 在 Manifest 中注册服务
<service android:name=".service.MyIntentService"/>
// 像启动 Service 那样启动 IntentService
Intent startIntent = new Intent(getActivity(), MyIntentService.class);
getActivity().startService(startIntent);
完整代码如下:
public class MyIntentService extends IntentService {
private boolean isRunning = true;
private int count = 0;
private LocalBroadcastManager mLocalBroadcastManager;
public MyIntentService() {
super("MyIntentService");
}
@Override
public void onCreate() {
super.onCreate();
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
sendServiceStatus("服务启动");
}
@Override
protected void onHandleIntent(Intent intent) {
try {
sendThreadStatus("线程启动", count);
Thread.sleep(1_000);
sendServiceStatus("服务运行中...");
isRunning = true;
count = 0;
while (isRunning) {
count++;
if (count >= 100) {
isRunning = false;
}
Thread.sleep(1000);
sendThreadStatus("线程运行中...", count);
}
sendThreadStatus("线程结束", count);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
sendServiceStatus("服务结束");
}
// 发送服务状态信息
private void sendServiceStatus(String status) {
Intent intent = new Intent(IntentServiceFragment.ACTION_TYPE_SERVICE);
intent.putExtra("status", status);
mLocalBroadcastManager.sendBroadcast(intent);
}
// 发送线程状态信息
private void sendThreadStatus(String status, int progress) {
Intent intent = new Intent(IntentServiceFragment.ACTION_TYPE_THREAD);
intent.putExtra("status", status);
intent.putExtra("progress", progress);
mLocalBroadcastManager.sendBroadcast(intent);
}
}
通过用IntentService使得这个线程先运行完才会开启其他的线程,这也就是IntentService的区别和作用