学习笔记03.浅析service
来源:互联网 发布:看摄像头监控软件 编辑:程序博客网 时间:2024/05/21 05:21
作为Android四大组件之一,service在每一个应用程序中都扮演着非常重要的角色。它主要用于在后台处理一些耗时的逻辑,或者去执行某些需要长期运行的任务。必要的时候
我们甚至可以在程序退出的情况下,让Service在后台继续保持运行状态。
一、新建一个service
新建一个service很简单,只需要让我们自定义的MyService继承Service类并复写主要的几个方法(onCreate()、onStartCommand()、onDestroy()、onBind())即可。
二、开启和关闭一个service
1.点击按钮开启service
Intent startIntent = new Intent(this, MyService.class);
startService(startIntent); 此时 onCreate()、onStartCommand() 方法会依次执行,再次点击 onStartCommand() 方法会再次执行,而onCreate()不会
2.点击按钮停止service
Intent stopIntent = new Intent(this, MyService.class);
stopService(stopIntent); onDestroy()方法会被执行。
说到这里我们只是单单开启了一个服务做一些耗时的工作,但是在activity中我们并不知道service中的工作做的怎么样了,比如任务的进度,任务的持续时间等等和该任务相关
的信息。为了加强service和activity的联系引入 绑定service --bindService。
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myBinder = (MyService.MyBinder) service;
myBinder.startDownload();
}
};
Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
这里需要传入三个参数 第一个 intent就不说了,第二个参数是前面创建出的ServiceConnection的实例,第三个参数是一个标志位,这里传入BIND_AUTO_CREATE表示在
Activity和Service建立关联后自动创建Service(一般会默认选择此标志位)
我们回到上面 创建ServiceConnection的地方 这里需要复写两个方法 onServiceDisconnected 、onServiceConnected
当service和activity断开连接和建立连接时会分别调用这两个方法。
public void onServiceConnected(ComponentName name, IBinder service)
中service是从onBind()方法return过来的,我们拿到这个实例即可在activity中调用MyService中的方法,达到activity和Service通信的目的。
例如:
public class MyService extends Service {
public static final String TAG = "MyService";
private MyBinder mBinder = new MyBinder();
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate() executed");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand() executed");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy() executed");
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
class MyBinder extends Binder {
public void startDownload() {
Log.d("TAG", "startDownload() executed");
}
}
}
activity中拿到IBinder 实例调用service中的方法。
public class MainActivity extends Activity implements OnClickListener {
private Button startService;
private Button stopService;
private Button bindService;
private Button unbindService;
private MyService.MyBinder myBinder;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myBinder = (MyService.MyBinder) service;
myBinder.startDownload();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService = (Button) findViewById(R.id.start_service);
stopService = (Button) findViewById(R.id.stop_service);
bindService = (Button) findViewById(R.id.bind_service);
unbindService = (Button) findViewById(R.id.unbind_service);
startService.setOnClickListener(this);
stopService.setOnClickListener(this);
bindService.setOnClickListener(this);
unbindService.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_service:
Intent startIntent = new Intent(this, MyService.class);
startService(startIntent);
break;
case R.id.stop_service:
Intent stopIntent = new Intent(this, MyService.class);
stopService(stopIntent);
break;
case R.id.bind_service:
Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
break;
case R.id.unbind_service:
unbindService(connection);
break;
default:
break;
}
}
}
如上面的代码解除绑定只需: unbindService(connection);
另外一种情况既点击了startService 和bindService需要 在绑定的activity不被销毁的情况下stopService(stopIntent) 和unbindService(connection) 都执行后 service才会被销
毁。 如果绑定的activity被销毁 对应的绑定的service也会被销毁。
那有没有一种service 在执行完我们需要的任务后就自动销毁呢?答案是肯定的它就就是 IntentService
IntentService是继承service 并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统的Service一样,同时,当任务
执行完后,IntentService会自动停止,而不需要我们手动去控制或stop。另外,可以启动IntentService多次,而每一个耗时操作会以工作队列的方式在IntentService的
onHandleIntent回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个,以此类推。
远程Service 它的坑比较多,一般能不用就不用,如果有兴趣的小伙伴可以百度
Android Service完全解析,关于服务你所需知道的一切(下)
小伙伴发现有问题的地方请及时提出来,大家一起学习
O(∩_∩)O谢谢!
- 学习笔记03.浅析service
- Web service学习笔记
- Web Service学习笔记
- web service学习笔记
- Web service学习笔记
- Web Service学习笔记
- Android Service学习笔记
- Web Service学习笔记
- Web Service学习笔记
- web service 学习笔记
- Web Service学习笔记
- Android Service学习笔记
- Web Service学习笔记
- Service学习笔记
- Web Service学习笔记
- Android Service学习笔记
- web service 学习笔记
- Web Service学习笔记
- 三元运算符
- throw "uglifyjs is deprecated
- H264关于RTP协议的实现
- a different object with the same identifier value was already associated with the session错误
- CDH HDFS failover 配置
- 学习笔记03.浅析service
- RILD及qcril初始化流程
- 微信小程序 template添加绑定事件
- 使用 Python Scrapy 爬取微博内容【一】
- [Python3.x]网络爬虫(一):利用urllib通过指定的URL抓取网页内容
- Mybatis 总结
- 大华摄像头获取yuv数据
- 计算算法的时间复杂度和空间复杂度
- 深度学习入门(二) 线性回归、逻辑回归、正则化过程、神经网络简介