什么是Service
1.Service是Android四大组件之一,和Activity的级别相当。
2.Service是可以长时间,运行在后台的,是不可见,是没有界面的组件。
3.Service是运行在主线程中的
4.Service可以跨进程调用
为什么要学习Service
1.大部分病毒利用service的特点,可以在不知不觉中完成预设的功能
2.作为安卓工程师,至少需要了解service是如何在后台完成预设功能
3.优先级比较高
4.在某个功能需要执行很长时间,并且在这个执行过程中,不需要让用户操作,不需要在Activity进行交互的情况下,可以通过Service在后台完成指定任务
Service有哪些应用场景
1.要在后台
2.耗时比较久
如何使用Service
- startService
使用startService方式启动Service
1.新建类继承Service
package com.example.myapplication.myService;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Created by lyj on 2018/6/19.
*/
public class MyService extends Service {
private int count=0;
@Override
public void onCreate() {
super.onCreate();
}
@Override
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);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("onDestroy", "销毁");
}
}
2.重写onCreate方法
public void onCreate() {
super.onCreate();
}
3.实现onBind抽象方法
public IBinder onBind(Intent intent) {
return null;
}
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.重写onDestory方法
6.在AndroidManifest中注册
<activity android:name=".myService.MyServiceActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".mybindService.BindService">
<action android:name="com.example.myapplication.myService.MyService" />
</service>
7.在有context环境中通过startService启动Service
Intent intent = new Intent(MyServiceActivity.this, MyService.class);
startService(intent);//启动service
8.在有context环境中通过stopService停止Service
Intent intent2 = new Intent(MyServiceActivity.this, MyService.class);
stopService(intent2);//停止service
新建类的完整代码:
package com.example.myapplication.myService;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Created by lyj on 2018/6/19.
*/
public class MyService extends Service {
private int count=0;
@Override
public void onCreate() {
super.onCreate();
}
@Override
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);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("onDestroy", "销毁");
}
}
activity类完整代码:
package com.example.myapplication.myService;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.example.myapplication.R;
public class MyServiceActivity extends AppCompatActivity implements View.OnClickListener {
private Button myservice_btn, myservice_start, myservice_stop;
public static ImageView myservice_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_service);
myservice_btn = findViewById(R.id.myservice_btn);
myservice_btn.setOnClickListener(this);
myservice_start = findViewById(R.id.myservice_start);
myservice_start.setOnClickListener(this);
myservice_stop = findViewById(R.id.myservice_stop);
myservice_stop.setOnClickListener(this);
myservice_image = findViewById(R.id.myservice_image);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
// case R.id.myservice_btn:
// Intent intent=new Intent(MyServiceActivity.this,MyService.class);
// startService(intent);//启动service
// break;
case R.id.myservice_start:
Intent intent = new Intent(MyServiceActivity.this, MyService.class);
startService(intent);//启动service
break;
case R.id.myservice_stop:
Intent intent2 = new Intent(MyServiceActivity.this, MyService.class);
stopService(intent2);//停止service
break;
default:
break;
}
}
}
startService的生命周期
context.startService()–>onCreate()–>onStartCommand()–>context.stopService()–>onDestory()–>Service stop
- bindService
销毁之前一定要解绑
使用bindService方式绑定Service的步骤
1.新建类继承Service
2.重写onCreate方法
3.实现onBind抽象方法
在BindService类里新建内部类MyBinder,继承自Binder(Binder实现IBinder接口)。MyBinder提供方法返回BindService实例。
public class MyBinder extends Binder {
BindService getService() {
return BindService.this;
}
}
实例化MyBinder得到mybinder对象
private final IBinder binder = new MyBinder();
实现onBind抽象方法
public IBinder onBind(Intent intent) {
return binder;
}
4.重写onUnbind方法
5.重写onDestory方法
6.在AndroidManifest中注册
7.在有context环境中实现ServiceConnection接口,重写onServiceConnected()和onServiceDisconnected()方法
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连接成功");
// 执行Service内部自己的方法
bindService.excute();
}
};
8.在有context环境中通过bindService绑定Service
Intent intent = new Intent(this, BindService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
9.在有context环境中通过unbindService解绑Service
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
bindService方法
1.Intent对象,用于传参
2.ServiceConnection接口对象
3.flag标识,常用的flag是Context.BIND_AUTO_CREATE
bindService的生命周期
context.startService()–>onCreate()–>onBind()–>Service running()–>onUnbind()–>onDestory()–>Service stop
IntentService有什么不同
bindService,startService都是运行在主线程中,如果有耗时操作,需要我们自己开线程并维护线程,IntentService不需要开启子线程
IntentService怎么用,注意事项
- 使用IntentService的步骤
1.新建类继承IntentService类
public class MyIntentService extends IntentService{
private int count=0;
public MyIntentService() {
super("");
}
public MyIntentService(String name) {
super(name);
}
}
2.实现父类的构造方法
public MyIntentService(String name) {
super(name);
}
3.重写onHandleIntent()方法
@Override
protected void onHandleIntent(@Nullable Intent intent) {
while (count<100){
count++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
其他步骤和上面的startService差不多
新建类的完整代码:
package com.example.myapplication.myIntentService;
import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Created by lyj on 2018/6/19.
*/
public class MyIntentService extends IntentService{
public MyIntentService() {
super("");
}
public MyIntentService(String name) {
super(name);
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
int count=0;
while (count<100){
count++;
Log.d("MyIntentService", "onHandleIntent: "+count);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
activity类完整代码:
package com.example.myapplication.myIntentService;
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;
}
}
}
- 主要事项
1.注册时候如果不加一个无参的构造方法注册表就会报错
无参构造方法这样写:
public MyIntentService() {
super("");
}
2.启动Service只能用start启动Service