解决方案 »

  1.   

    private  Intent intent;
    private  TimerService timerService;
     
    public TimerService getService(Context context, String serviceName) {
     
        intent = new Intent(serviceName);
        System.out.println("isSuccess"+context.getApplicationContext().bindService(intent, conn, Context.BIND_AUTO_CREATE));
        return this.timerService;
    }你那return的timerService就是你自己定义的那个,你定义的那个本来就是null啊.
      

  2.   

    但是context.getApplicationContext().bindService(intent, conn, Context.BIND_AUTO_CREATE)不是给他赋值了么
      

  3.   

    但是context.getApplicationContext().bindService(intent, conn, Context.BIND_AUTO_CREATE)不是给他赋值了么你把代码贴全吧,都看不到别的地方,bind后,conn中get一下service.还有manifest文件中service配置是否正确等.
      

  4.   

    公共方法:public class CalibrationTimer { private  Intent intent;
    private  TimerService timerService; public CalibrationTimer(Context context) { intent = new Intent("com.example.service.timerService");
    System.out.println("hhhh"+context.getApplicationContext().bindService(intent, conn, Context.BIND_AUTO_CREATE));

    } private  ServiceConnection conn = new ServiceConnection() { @Override
    public void onServiceDisconnected(ComponentName name) {
    // TODO Auto-generated method stub } @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
    // TODO Auto-generated method stub
    timerService = ((TimerService.MyBinder)service).getService();
    System.out.println("conn");
    }
    };
    public void setTimer(){
    timerService.setServerTime();
    }}
      

  5.   

    manifest        <service android:name="com.example.service.TimerService" android:exported="false">
                <intent-filter >
                    <action android:name="com.example.service.timerService" android:exported="false"/>
                    
                </intent-filter>
            </service>
      

  6.   

    兄弟你把那个 return this.timerService;放在  public void onServiceConnected(ComponentName name, IBinder service) 这个方法里面稳稳的,如果能直接那样调用那回掉方法就没啥用了啊。连接是需要时间的,不是秒连的
      

  7.   

    public TimerService onServiceConnected(ComponentName name, IBinder service)
    这样?
    那外面这么调用啊?
      

  8.   

    public TimerService onServiceConnected(ComponentName name, IBinder service)
    这样?
    那外面这么调用啊?
      

  9.   

    在调用 bindService 并 return 的时候, onServiceConnected 还没有执行到啊, 这是一个异步操作.
    在安卓中异步操作很常见,比较好的办法就是传递一个接口给 getService 存下来, 在 onServiceConnected 的时候进行回调.
    Runnable 就可以.