服务绑定到一个活动 [英] Service bind to an Activity

查看:191
本文介绍了服务绑定到一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code:

 公共类MainActivity延伸活动{
    私人组件名MSERVICE;
    私人SERVICIO的ServiceBinder;    私人ServiceConnection mConnection =新ServiceConnection(){
           公共无效onServiceConnected(组件名的className,服务的IBinder){
             的ServiceBinder =((Servicio.MyBinder)服务).getService();
           }           公共无效onServiceDisconnected(组件名的className){
             的ServiceBinder = NULL;
           }
        };    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        意图bindIntent =新意图(这一点,Servicio.class);
        bindService(bindIntent,mConnection,Context.BIND_AUTO_CREATE);
    }    @覆盖
    保护无效调用onStart(){
        serviceBinder.somethingThatTakesTooMuch();
        super.onStart();
    }公共类SERVICIO延伸服务{
    私人最终的IBinder粘结剂=新MyBinder();    @覆盖
            公众的IBinder onBind(意向意图){
    返回粘结剂;
    }    公众诠释somethingThatTakesTooMuch(){
        返回1;
    }    公共类MyBinder扩展粘结剂{
          SERVICIO的getService(){
            返回Servicio.this;
          }
    }

当我运行它,
它得到一个NullPointerException异常在这一行:

  serviceBinder.somethingThatTakesTooMuch();


解决方案

在onStart 被调用之前,该服务的连接完成。这不是瞬间。

您只能保证服务连接后,您的onServiceConnected被调用。只有这样,你打电话的ServiceBinder方法。

尝试调用 serviceBinder.somethingThatTakesTooMuch()的ServiceBinder =((Servicio.MyBinder)服务)之后的行.getService();

This is my code:

public class MainActivity extends Activity {
    private ComponentName mService;
    private Servicio serviceBinder;

    private ServiceConnection mConnection = new ServiceConnection() {
           public void onServiceConnected(ComponentName className, IBinder service) {
             serviceBinder = ((Servicio.MyBinder)service).getService();
           }

           public void onServiceDisconnected(ComponentName className) {
             serviceBinder = null;
           }
        };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent bindIntent = new Intent(this, Servicio.class);
        bindService(bindIntent, mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStart() {
        serviceBinder.somethingThatTakesTooMuch();
        super.onStart();
    }



public class Servicio extends Service {
    private final IBinder binder = new MyBinder();

    @Override
            public IBinder onBind(Intent intent) {
    return binder;
    }

    public int somethingThatTakesTooMuch() {
        return 1;
    }

    public class MyBinder extends Binder {
          Servicio getService() {
            return Servicio.this;
          }
    }

When I run it, It get a NullPointerException in this line:

serviceBinder.somethingThatTakesTooMuch();

解决方案

Your onStart is being called before the connection to the service is complete. It's not instant.

You can only guarantee that the service is connected AFTER your onServiceConnected is called. Only then can you call methods on serviceBinder.

Try calling serviceBinder.somethingThatTakesTooMuch() on the line after serviceBinder = ((Servicio.MyBinder)service).getService();

这篇关于服务绑定到一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆