aidl服务 android,android – 在bindService()之后没有连接的AIDL服务

在尝试使用AIDL进行服务与客户端应用程序交互时遇到了问题。服务启动并显示在运行服务列表中,但客户端调用bindService()始终返回false,ServiceConnection的onServiceConnected()未被调用。服务端的onBind()方法也没有执行。检查日志没有发现明显错误。问题可能在于服务绑定过程中的配置或实现细节。
摘要由CSDN通过智能技术生成

我正在尝试使用AIDL开发2个应用程序(服务应用客户端应用程序)的设置.我目前有3个模块的设置:

> android-agent-framework(只保存AIDL文件的android库模块)

> android-agent(服务)

> android-example-client(客户端)

android-agent和android-agent-framework对第一个访问接口的依赖.

每当客户端调用bindService()时,它都会返回false,而在ServiceConnection中,不会调用onServiceConnected().同样在服务实现中,不调用onBind().日志中没有错误.

这是代码:

android-agent活动:

public class MyCompanyStartActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

Log.i(MyCompanyStartActivity.class.toString(),"Create MyCompanyStartActivity");

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ComponentName service = startService(new Intent(this,MyCompanyRequestService.class));

Log.i("tag",service.getClassName() + "::" + service.getPackageName());

}

}

android-agent服务:

public class MyCompanyRequestService extends Service {

@Override

public IBinder onBind(Intent intent) {

Log.i(MyCompanyRequestService.class.toString(),"Starting SmartRest Service");

return mBinder;

}

private final IMyCompanyRequestService.Stub mBinder = new IMyCompanyRequestService.Stub() {

@Override

public void sendData(String xid,String authentication,String data) throws RemoteException{

Log.i(MyCompanyRequestService.class.toString(),"sending data: " + data);

}

};

}

android-agent清单:

package="com.mycompany.android.agent" >

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name=".MyCompanyStartActivity"

android:label="@string/app_name" >

android:name="com.mycompany.android.agent.framework.MyCompanyRequestService"

android:process=":remote"

android:exported="true"

android:enabled="true">

android-example-client活动:

public class ClientStarter extends Activity {

protected IMyCompanyRequestService mycompanyRequestService = null;

@Override

public void onCreate(Bundle savedInstanceState) {

Log.i("tag","create client");

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

@Override

protected void onStart() {

super.onStart();

if (mycompanyRequestService == null) {

printServices();

Intent it = new Intent("MyCompanyRequestService");

it.setPackage("com.mycompany.android.agent.framework");

Log.i("tag","before binding service: " + it.getAction() + "::" + it.getPackage());

boolean serviceBinding = getApplicationContext().bindService(it,connection,Context.BIND_AUTO_CREATE);

Log.i("tag","service is bound: " + serviceBinding);

}

Handler handler = new Handler();

handler.postDelayed(new Runner(),10000);

}

@Override

protected void onDestroy() {

super.onDestroy();

unbindService(connection);

}

private ServiceConnection connection = new ServiceConnection() {

@Override

public void onServiceConnected(ComponentName name,IBinder service) {

Log.i("service","Service connected");

mycompanyRequestService = IMyCompanyRequestService.Stub.asInterface(service);

Toast.makeText(getApplicationContext(),"Service Connected",Toast.LENGTH_SHORT).show();

Log.i("service","Service connected");

}

@Override

public void onServiceDisconnected(ComponentName name) {

Log.i("service","Service disconnected");

mycompanyRequestService = null;

Toast.makeText(getApplicationContext(),"Service Disconnected","Service disconnected");

}

};

private void printServices() {

ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {

Log.d("service",service.service.getClassName());

}

}

private class Runner implements Runnable {

@Override

public void run() {

Log.i("tag","starting");

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Location loc;

try {

Thread.sleep(10000);

} catch (InterruptedException e) {

Log.e(ClientStarter.class.toString(),"Error",e);

} while(true) {

try {

if (mycompanyRequestService != null) {

loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

Log.i(ClientStarter.class.toString(),loc.getLatitude() + " - " + loc.getLongitude() + " - " + loc.getAltitude());

mycompanyRequestService.sendData("test","auth",String.valueOf(loc.getLatitude()) + "," + String.valueOf(loc.getLongitude()) + "," + String.valueOf(loc.getAltitude()));

} else {

Log.i(ClientStarter.class.toString(),"service not yet available");

}

Thread.sleep(5000);

} catch (InterruptedException e) {

Log.e(ClientStarter.class.toString(),e);

} catch (RemoteException e) {

Log.e(ClientStarter.class.toString(),e);

}

}

}

}

}

尝试绑定服务之前的printServices()调用实际上列出了服务,因此它正在运行.

日志不包含任何错误,客户端最终在循环中运行,但服务仍为空.

也许有人之前遇到过类似的问题.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值