이 문제에 대한 다른 모든 StackOverflow 게시물을 참조 했으므로 제대로 작동하지 않습니다.두 앱 사이에서 AIDL을 사용할 때 onServiceConnected()가 호출되지 않음
내 목표는 주요 활동이없는 앱을 작성하는 것입니다. 별도의 앱에서 바인딩하고 AIDL을 사용하여 상호 작용할 수 있어야합니다.
나는 이전에 동일한 앱에서 호출 한 서비스와 활동을 모두 가지고 있었으며 완벽하게 작동했습니다. 하지만 두 개의 서로 다른 앱 사이에서 작동하도록해야합니다.
서비스의 onBind()는 다음과 같습니다
@Nullable
@Override
public IBinder onBind(Intent intent) {
// This binds the bluetooth service itself to the logger service, do this when the logger service itself is bound
Intent serviceIntent = new Intent(this, BluetoothService.class);
intent.setPackage(this.getPackageName());
Log.d(getClass().getName(), "started");
if(bluetoothService == null) {
if (this.bindService(serviceIntent, bluetoothServiceConnection, BIND_AUTO_CREATE)) {
Log.d(getClass().getName(), "returned the service");
return mBinder;
} else {
Log.d(getClass().getName(), "returned null");
return null;
}
} else {
Log.d(getClass().getName(), "returned the service");
return mBinder;
}
}
자체가 다른 서비스와 결합 서비스,하지만 블루투스 물건의 모든 이미 작동합니다. 서비스와 응용 프로그램의
매니페스트는 다음과 같습니다
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:label="@string/app_name">
<service
android:label="LoggerService"
android:name="io.modum.ble.LoggerService"
android:exported="true">
<intent-filter>
<action android:name="io.modum.ble.LoggerService" />
</intent-filter>
</service>
<service android:name="io.modum.ble.service.BluetoothService" />
</application>
내 활동에
, 나는 이런 식으로 바인딩 :
Intent intent = new Intent();
ComponentName componentName = new ComponentName("io.modum.modum_ble_service", "io.modum.ble.LoggerService");
intent.setComponent(componentName);
boolean success = getApplicationContext().bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(getClass().getName(), "Service connected");
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}, BIND_AUTO_CREATE);
Log.d(getClass().getName(), String.valueOf(success));
AIDL 파일은 두 응용 프로그램에서 모두 동일한 패키지 이름을 갖습니다.
아무런 예외가 발생하지 않고 onServiceConnected가 호출되지 않고 모든 것이 작동합니다.
bindService()는 true를 반환하고 서비스가 의도를 받아 올바르게 시작되고 "서비스가 반환되었습니다"는 기록되지만 onServiceConnected()는 호출되지 않습니다.
백그라운드 앱을 실행하기 전에 Android 앱에 사용자 인터페이스가 있어야합니다. 예를 들어 문제가있을 수 있습니다. https://stackoverflow.com/questions/8531926/how-to-start-a-service-when-apk-is-installed-for-the-first-time을 참조하십시오. –
@ScottKronheim 나는 그것을 달리고있다. 외관상으로는 당신은 구성 요소 이외에 서비스 응용 프로그램의 패키지로 패키지를 설정해야합니다. 서비스 응용 프로그램이 설치 되 자마자 서비스를 실행할 필요가 없습니다. 다른 앱이 바인딩하는 즉시 서비스가 실행되어야합니다. – MethDamon