private ServiceConnection conn = new ServiceConnection()

{

@Override

public void onServiceDisconnected(ComponentName name)

{

// TODO Auto-generated method stub

}


@Override

public void onServiceConnected(ComponentName name, IBinder service)

{

binder = (MyBinder) service;

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

Fragment fragment = new PlayIndex(binder.cur,binder.list);

ft.replace(R.id.play, fragment);

ft.commit();

}

};


@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.play);


Intent intent = new Intent(Play.this, PlayService.class);

bindService(intent, conn, Context.BIND_AUTO_CREATE);




}


bindService的调用是异步的,也就是说到service的链接不是在bindService被调用后就马上完成的。不了解这一点会导致一些问题,例如:虽然在bindService.onServiceConnected中有对binder的赋值,但如果以为在onCreate中的bindService之后onServiceConnected就已经被调用了,就会出现问题,因为其实这时binder仍然是null,到service的链接会在之后的某个时间点被建立,建立链接后系统会调用onServiceConnected这个函数,所以可以在这个函数中放置一些代码来提醒当前activity到service的链接已经建立完毕。但具体最晚到什么时候还不知道,猜测应该是onCreate执行完毕后吧。

更多相关文章

  1. android 联系人搜索
  2. Android(安卓)Uri.getQueryParameter使用注意
  3. 调用getChildFragmentManager时出现的Bug
  4. Android(安卓)直接通过JNI访问驱动
  5. Android(安卓)SQLite数据库增删改查
  6. android支付宝 KeyFactory PrivateKey
  7. android个推消息推送,asp.net调用接口
  8. Android(安卓)API Level 与 Platform Version之间的关系
  9. DrawLayout几个注意点

随机推荐

  1. android的ViewFlipper
  2. Android 实现水波纹动效
  3. Android Studio对话框登录。。。
  4. 来往 android客户端发布
  5. android属性android:stateNotNeeded
  6. [Android JNI] JNI Types and Data Struc
  7. android启动模式
  8. Android 计算器界面的实现
  9. Android clipChildren用法
  10. AndroidStudio开发flutter之环境配置