Android:bindService() 出现问题 ->服务为空

发布于 2025-01-04 00:05:02 字数 1343 浏览 0 评论 0原文

我在将服务绑定到活动时遇到问题。我得到playing_service==null。我找不到我做错了什么。为什么playing_service为空?

MyActivity 类:

private playService playing_service=null;

private ServiceConnection service_conn=new ServiceConnection(){
    public void onServiceConnected(ComponentName className, IBinder service) {
        LocalBinder binder=(LocalBinder)service;
        playing_service=binder.getService();
    }
    public void onServiceDisconnected(ComponentName arg0) {
        // TODO Auto-generated method stub

    }
};

public void playTrack(View view){       
        Intent i=new Intent(this,playService.class);
        i.setAction("com.c0dehunter.soundrelaxer.PLAY");
        bindService(i,service_conn,Context.BIND_AUTO_CREATE);

        if(playing_service==null) //here I get true,
             //if I try to access playing_service I get NullPointerException

    }
}

playService 类:

private final IBinder binder=new LocalBinder();

public int onStartCommand(Intent intent, int flags, int startId){       
     return 1; //dummy
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return binder;
}

public class LocalBinder extends Binder{
    public playService getService(){
        return playService.this;
    }
}

I'm having a problem with binding service to an activity. I get playing_service==null. I can't find what I'm doing wrong. Why is playing_service null??

MyActivity class:

private playService playing_service=null;

private ServiceConnection service_conn=new ServiceConnection(){
    public void onServiceConnected(ComponentName className, IBinder service) {
        LocalBinder binder=(LocalBinder)service;
        playing_service=binder.getService();
    }
    public void onServiceDisconnected(ComponentName arg0) {
        // TODO Auto-generated method stub

    }
};

public void playTrack(View view){       
        Intent i=new Intent(this,playService.class);
        i.setAction("com.c0dehunter.soundrelaxer.PLAY");
        bindService(i,service_conn,Context.BIND_AUTO_CREATE);

        if(playing_service==null) //here I get true,
             //if I try to access playing_service I get NullPointerException

    }
}

playService class:

private final IBinder binder=new LocalBinder();

public int onStartCommand(Intent intent, int flags, int startId){       
     return 1; //dummy
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return binder;
}

public class LocalBinder extends Binder{
    public playService getService(){
        return playService.this;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情废物 2025-01-11 00:05:02

您的服务可能不为空,因为绑定服务是一种异步方法,因此您不应在调用绑定方法后检查服务的可用性,而是应该在服务连接实现中执行此操作,例如:

private ServiceConnection service_conn=new ServiceConnection(){
    public void onServiceConnected(ComponentName className, IBinder service) {
        LocalBinder binder=(LocalBinder)service;
        playing_service=binder.getService();

        if(playing_service != null){
            Log.i("service-bind", "Service is bonded successfully!");

            //do whatever you want to do after successful binding
        }
    }
    public void onServiceDisconnected(ComponentName arg0) {
        // TODO Auto-generated method stub

    }
};

Your service might not be null because binding a service is an asynchronous method, so instead of checking the availability of your service yet after calling the bind method, you should do it in your service connection implementation, for e.g.:

private ServiceConnection service_conn=new ServiceConnection(){
    public void onServiceConnected(ComponentName className, IBinder service) {
        LocalBinder binder=(LocalBinder)service;
        playing_service=binder.getService();

        if(playing_service != null){
            Log.i("service-bind", "Service is bonded successfully!");

            //do whatever you want to do after successful binding
        }
    }
    public void onServiceDisconnected(ComponentName arg0) {
        // TODO Auto-generated method stub

    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文