1.Activity绑定Service语句最好写在onCreat()中,绑定后会直接调用onServiceConnected方法,可以将执行语句写在该方法中。
private ServiceConnection trackcon = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub TrackBinder myBinder = (TrackBinder) service; trackService = myBinder.getService(); Timer updateTimer = new Timer("trackService"); updateTimer.scheduleAtFixedRate(new TimerTask() { public void run() { handler.post(trackRunnable); } }, 0, 150); } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub trackService = null; } };
2.将执行语句放在onstart中时导致空指针,不知道原因……可将其放在按钮等的处理程序中。