所以我设置了绑定服务,但每次拨打bindService()
时,它都不会调用我的服务的onBind()
方法,而onServiceConnected()
永远不会被调用。我没有看到任何错误,没有任何错误。
private LoggingService mService;
private boolean mBound = false;
private boolean mLogging = false;
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service){
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0){
mBound = false;
}
};
...
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if(!mBound){
Intent intent = new Intent(getActivity(), LoggingService.class);
getActivity().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
}
我把断点都放在了这一点上。 bindService()
肯定会被调用,但无论我在onServiceConnected()
中等待多久,或者我的服务中的任何内容都被调用。
所以当我到达这里时,会抛出一个nullPointerException
logging.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mLogging){
Toast toast = Toast.makeText(getActivity(),
"Service stopping", Toast.LENGTH_LONG);
toast.show();
mService.stopLogging();
mLogging = false;
} else {
Toast toast = Toast.makeText(getActivity(),
"Service Starting", Toast.LENGTH_LONG);
toast.show();
mService.startLogging("dude.jsn");
mLogging = true;
}
}
});
记录是Button
。不知道我错过了什么。这是从片段中调用的,但这不重要吗?上下文仍然有效。
public class LoggingService extends Service {
public static final int LOG_INTERVAL = 500;
LocalBinder mBinder = new LocalBinder();
LoggingThread thread;
public class LocalBinder extends Binder {
LoggingService getService() {
return LoggingService.this;
}
}
...
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
指出我错过的任何帮助都会很棒。