此代码可在Android Lollipop,Marshmallow和Oreo中使用。但不是在奇特卡特。
当我在应用程序类中调用以下代码时。
m_MainService_Interface = new MainService_Interface(getApplicationContext());
我的代码
public MainService_Interface(Context context) {
mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
m_MainService = ((MainService.MainServiceBinder) service).getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
mServiceConnection = null;
m_MainService = null;
}
};
context.bindService(new Intent(context, MainService.class)
.setPackage(context.getPackageName()), mServiceConnection, Context.BIND_AUTO_CREATE);
}
mServiceConnection = new ServiceConnection
中发生错误。
@Override
public void onCreate() {
super.onCreate();
m_MyApplication = this;
m_MainService_Interface = new MainService_Interface(getApplicationContext());
}
MainService_Interface class
public MainService_Interface(Context context) {
mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
m_MainService = ((MainService.MainServiceBinder) service).getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
mServiceConnection = null;
m_MainService = null;
}
};
context.bindService(new Intent(context, MainService.class)
.setPackage(context.getPackageName()), mServiceConnection, Context.BIND_AUTO_CREATE);
}