关于Service的线程问题
上一篇说了Service的基本使用:
Android:Service(一)——Service的基本使用,其中有一点没有提到的是:在service上运行的任务也是运行在主线程上的,所以不要以为开启了服务就可以在里面尽情地做一些耗时任务。不然的话也会导致ui卡顿~可以做这么一个实验,在Activity上输出当前线程的Id然后在Service上也输出当前线程的id,就会发现他们的id是一致的,也就是说在service 上也是主线程在运行~因此若要在service上运行耗时任务记得先开一条子线程,然后在这子线程上运行。
关于绑定Service
用startService开启的服务,一旦开启之后,该service与activity就没有关系了。也就是说,activity很难调用到service上的方法,很难通过用户在activity上的操作去改变service的任务。同时,activity销毁也与service无关。activity销毁了之后service还在运行~
如果是通过绑定service启动service那么就不存在这些问题了。
接下来就说说如何绑定Service,
还是跟平常一样新建一个Service,可以看到刚新建的Service里面有一个onBind方法,在Activity进行绑定Service之后会把onBind所返回的对象返回到onServiceConnected,我们只需要在该回调里接收所返回的对象就可以了。我们就是根据这个返回的对象来操作service里面的方法。因此我们先新建一个内部类,把将要调用的方法都写在这个内部类里面,这里我写了个log的方法作为示例。上面提到onBind会把一个对象返回到onServiceConnected里,所以我们把刚定义好的内部类的对象放到onBind里面,并且返回出去。我们可以看到onBind的返回值是IBinder,因此我们需要让新建的内部类实现Ibinder接口,但是我们会发现Ibinder接口实在有太多方法需要实现,这个时候我们可以选择继承一个默认的实现类Binder。
package com.example.javy.bindservicedemo;
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 {
@Override
public IBinder onBind(Intent intent) {
Log.e("bind","onBind");
return new MyBind();
}
public class MyBind extends Binder{
public void logMessage(String msg){
Log.e("testlog",msg);
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("serviceDes","onDestroy");
}
}
好了service已经写好了,接下来我们就要看看activity到底要怎样写才能绑定service~
首先先上布局文件,布局文件只是为了方便测试使用~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:text="绑定"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mybtnbind"
/>
<Button
android:text="解绑"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/unbind"
/>
<Button
android:text="Log"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/log"
/>
</LinearLayout>
然后看java代码:
package com.example.javy.bindservicedemo;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button btnBind;
private Button btnUnbind;
private MyConn conn;
private MyService.MyBind mybind;
private Button btnLog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
btnBind=(Button)findViewById(R.id.mybtnbind);
btnUnbind= (Button) findViewById(R.id.unbind);
btnLog= (Button) findViewById(R.id.log);
btnBind.setOnClickListener(this);
btnUnbind.setOnClickListener(this);
btnLog.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(conn==null){
conn=new MyConn();
}
if(v.equals(btnBind)){
Intent intent =new Intent(this,MyService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
}
if(v.equals(btnUnbind)){
unbindService(conn);
}
if(btnLog.equals(v)){
mybind.logMessage("挖槽~还真的可以啊");
}
}
private class MyConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e("bindLog","bind");
mybind= (MyService.MyBind) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e("bindLog","unbind");
}
}
}
我们来看看绑定的操作:
if(v.equals(btnBind)){
Intent intent =new Intent(this,MyService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
}
跟普通的启动service一样,也是新new一个intent出来,不同的是所调用的方法不是startService而是bindService。
bindService有三个参数,第一个是传入intent,第二个传入ServiceConnection,第三个传入flag~上文的BIND_AUTO_CREATE的意思是,如果服务不存在则创建一个的意思,更多flag请自行了解,本文主要是讲述如何通过绑定service的方式启动service。我们再看看那个ServiceConnection,Activity定义了一个内部类MyConn继承了ServiceConnection,其中有两个回调方法,其中一个是与service建立起连接的时候回调的,另一个是与Service断开连接的时候回调的~
再看该内部类的两个回调方法,其中onServiceConnected传进两个参数,其中一个是IBinder,该参数正是在Service的onBind返回的值~经过强转,用已经定义好的mybind接收。
最后使用mybind调用service里的方法。。。
想要解绑service也非常简单,只需调用unbindService,并传入绑定时所传入的ServiceConnection就可以了。
通过这种方式启动的service,如果activity销毁了,所绑定的service也随之销毁。
并且一旦绑定后,如果解绑该service那么该service也会销毁。
好吧~碍于笔者表达能力关系表达得不太清楚~因此笔者上传一个demo给大家看看~~放心~免费的~~哈哈哈