gpt4 book ai didi

android aidl,无法绑定(bind)到服务?

转载 作者:行者123 更新时间:2023-11-30 04:11:09 25 4
gpt4 key购买 nike

有没有人知道为什么?

Activity

    private IExternalDeviceScannerService myService = null;
private ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
myService = IExternalDeviceScannerService.Stub.asInterface(arg1);
}

public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
......
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Intent intent = new Intent(this, ExternalDeviceScannerService.class);
bindService(intent, serviceConnection,
Context.BIND_AUTO_CREATE);
....

服务

class ScannerServiceAIDL extends IExternalDeviceScannerService.Stub {
.......
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new ScannerServiceAIDL();
}

即使我等了几秒钟,我的 Activity 也从未连接到该服务!

最佳答案

我测试了你的代码并执行了代码没有任何问题。我认为您在项目的任何地方都犯了任何错误。我成功执行的代码如下。

IExternalDeviceScannerService.aidl

package com.example.aidltest;

interface IExternalDeviceScannerService
{
int getIntValue();
}

list .xml

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="22" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ExternalDeviceScannerService"></service>
</application>

Activity

private IExternalDeviceScannerService myService = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, ExternalDeviceScannerService.class);
// Bind service here
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

protected void onDestroy() {
super.onDestroy();
unbindService(mConnection);
myService = null;
}

private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
myService = IExternalDeviceScannerService.Stub.asInterface(service);
if (myService != null){
try {
Log.i("Value from AIDL", myService.getIntValue() + "");
} catch (RemoteException e) {
e.printStackTrace();
}
}
}

public void onServiceDisconnected(ComponentName className) {
myService = null;
}
};

服务

public class ExternalDeviceScannerService extends Service {

private static final String LOG_TAG = "Service";

@Override
public void onCreate() {
super.onCreate();
Log.i(LOG_TAG, "Service Started.");
}

@Override
public void onDestroy() {
super.onDestroy();
Log.i(LOG_TAG, "Service Stopped.");
}

final class ScannerServiceAIDL extends IExternalDeviceScannerService.Stub {
public int getIntValue() throws RemoteException {
return 20;
}
}
@Override
public IBinder onBind(Intent intent) {
Log.i(LOG_TAG, "Service Bound.");
return new ScannerServiceAIDL();
}
}

使用 AIDL 玩得开心!!!谢谢

关于android aidl,无法绑定(bind)到服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10832110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com