在binder客户端 activity代码如下
public class MainActivity extends Activity implements View.OnClickListener{
private IMusicPlayService stub;
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
stub=IMusicPlayService.Stub.asInterface(service);
Log.i("sihaixuan","onServiceConnected running");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
Intent intent=new Intent("com.android.sihaixuan.service");
bindService(intent, connection, Context.BIND_AUTO_CREATE);
Log.i("wuhong", "running here");
try {
String string="文件路径";
stub.start(string);
stub.stop();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
// Log.i("wuhong", "running here");
// try {
// String string="文件路径";
// stub.start(string);
// stub.stop();
// } catch (RemoteException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}
我把bindService()和stub.start()都放在onCreate()方法执行,运行程序报出空指针异常,发现stub的指为空,ServiceConnectinon的方法没有回调执行?怎回事?是不是activityThread在处理handlerbindserice 是通过handler异步处理调用onBind(),还没来得及把ibinder对象传给客户端??stub.start()就已经执行了
分享至: