安卓bindservice [英] android bindservice

查看:251
本文介绍了安卓bindservice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到空指针异常在行mService.start(),当我尝试结合已启动的服务。我从不同的活动(该服务被启动),同样的事情everythig去的权利。所有这些活动都是一个应用程序的一部分。

你觉得我做错了什么?

 公共类RouteOnMap扩展MapActivity {
    私有静态最终诠释NEW_LOCATION = 1;
    私有静态最终诠释GPS_OFF = 2;

    私人MapView类mMapView;
    私人ILocService MSERVICE;
    私人布尔mServiceStarted;
    私人布尔mBound;
    私人意图mServiceIntent;
    私人双人mLatitude,mLongitude;

    私人ServiceConnection连接=新ServiceConnection(){
        公共无效onServiceConnected(组件名的className,的IBinder iservice){
            MSERVICE = ILocService.Stub.asInterface(iservice);
            mBound = TRUE;
        }

        公共无效onServiceDisconnected(组件名的className){
            MSERVICE = NULL;
            mBound = FALSE;
        }

    };

    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.mapview);

        mMapView =(图形页面)findViewById(R.id.mapview);
        mMapView.setBuiltInZoomControls(真正的);
        mServiceIntent =新意图();
        mLatitude = 0.0;
        mLongitude = 0.0;
        mBound = FALSE;
    }

    @覆盖
    公共无效的OnStart(){
        super.onStart();

        mServiceIntent.setClass(这一点,LocationService.class);
        // startService(mServiceIntent);
        如果(!mBound){
            mBound = TRUE;
            this.bindService(mServiceIntent,连接,Context.BIND_AUTO_CREATE);
        }
    }

    @覆盖
    公共无效onResume(){
        super.onResume();


        尝试 {
            mService.start();
        }赶上(RemoteException的E){
            e.printStackTrace();
        }

    }

    @覆盖
    公共无效的onPause(){
        super.onPause();

        如果(mBound){
            this.unbindService(连接);
        }
    }

    @覆盖
    保护的布尔isRouteDisplayed(){
        // TODO自动生成方法存根
        返回false;
    }

}
 

解决方案

您有没有办法知道,如果该服务被绑定 onResume的() bindService()是不是阻塞调用。呼叫 mService.start() onServiceConnected()方法。

I get null pointer exception at line mService.start() when i try to bind to an already started service. I do the same thing from different activity(where the service gets started) everythig goes right. All these activities are part of one application.

What do you think I do wrong?

public class RouteOnMap extends MapActivity{
    private static final int NEW_LOCATION = 1;
    private static final int GPS_OFF = 2;

    private MapView mMapView;
    private ILocService mService;
    private boolean mServiceStarted;
    private boolean mBound;
    private Intent mServiceIntent;
    private double mLatitude, mLongitude;

    private ServiceConnection connection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder iservice) {
            mService = ILocService.Stub.asInterface(iservice);
            mBound = true;
        }

        public void onServiceDisconnected(ComponentName className) {
            mService = null;
            mBound = false;
        }

    };

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);

        mMapView = (MapView) findViewById(R.id.mapview);
        mMapView.setBuiltInZoomControls(true);      
        mServiceIntent = new Intent();
        mLatitude = 0.0;
        mLongitude = 0.0;
        mBound = false;
    }

    @Override
    public void onStart(){
        super.onStart();

        mServiceIntent.setClass(this, LocationService.class);
        //startService(mServiceIntent);
        if(!mBound){
            mBound = true;
            this.bindService(mServiceIntent, connection, Context.BIND_AUTO_CREATE);
        }
    }

    @Override
    public void onResume(){
        super.onResume();


        try {
            mService.start();
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onPause(){
        super.onPause();

        if(mBound){
            this.unbindService(connection);
        }
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}

解决方案

You have no way of knowing if the service is bound by onResume(). bindService() is not a blocking call. Call mService.start() from your onServiceConnected() method.

这篇关于安卓bindservice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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