我提到了有关此问题的所有其他StackOverflow帖子,但我无法使其正常工作。
我的目标是编写一个没有主要活动的应用程序,只需一项服务。您应该能够从单独的应用程序绑定到它并使用AIDL与它进行交互。
我之前有同一个应用程序中的服务和活动调用它,它运行完美。但我需要让它在两个单独的应用程序之间运行。
服务的onBind()如下所示:
userModel.getAuthStatus() means that user is logged
服务本身与另一项服务绑定,但所有蓝牙功能都已有效。
该服务的应用程序的清单如下所示:
@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>
AIDL文件在两个应用程序中都具有完全相同的包名称。
没有异常被调用,除了onServiceConnected之外一切正常。
bindService()返回true,服务接收到intent并正确启动,&#34;返回服务&#34;已记录,但永远不会调用onServiceConnected()。
答案 0 :(得分:0)
我让它运行,缺少的是
class ABC: UIViewController
{
var Distance : Int!
override func viewDidLoad()
{
super.viewDidLoad()
var obj_A = ABC()
obj_A.Distance = 10
var obj_B = objVR_A
obj_A.Distance = 30
print(obj_A.Distance) // 30
print(obj_B.Distance) // 30
var x = 10
let y = x
x = 30
print(x) //30
print(y) //10
}}
请注意,程序包名称是应用程序的名称,而不是服务所在的程序包。使用LogCat中显示的程序包名称。