bindService返回true可是我实现ServiceConnection的myService为空。下面是我的客服端代码
public class MainActivity extends AppCompatActivity {
private Intent services;
private IService myService;
private MyConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
services = new Intent();
services.setAction("com.wangjiang.studentinfomationservice");
services.setPackage("com.wangjiang.studentinfomationservice");
connection = new MyConnection();
boolean t = bindService(services, connection, BIND_AUTO_CREATE);
Toast.makeText(MainActivity.this, String.valueOf(t),Toast.LENGTH_LONG).show();
try {
if(myService !=null) {
List<Student> list =myService.getData();
if(list != null)
Toast.makeText(MainActivity.this,"list.size() + " , Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "NULL" , Toast.LENGTH_LONG).show();
} else
Toast.makeText(MainActivity.this, "NU" , Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
e.printStackTrace();
}
unbindService(connection);
}
private class MyConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
if (service == null)
Log.i("CLIENT","bind null");
else {
myService = IService.Stub.asInterface(service);
Log.i("CLIENT","bindetr");
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}