我创建了一个驻留在图书馆中的服务。然后将该库包含在另一个尝试启动该服务的项目中。我正在使用AIDL进行交互,但是当我尝试连接到服务时,我的onConnected方法永远不会受到攻击。
从日志文件中,我可以看到以下错误:
D/ActivityManager: bindService callerProcessName:com.something.services.dummyproject, calleePkgName: com.something.services.dummyproject, action: com.something.services.dummyservice.IDummyService
W/ActivityManager: Unable to start service Intent { act=com.something.services.dummyservice.IDummyService cmp=com.something.services.dummyproject/com.something.services.dummyservice.IDummyService } U=0: not found
D/ActivityManager: bindService callerProcessName:com.something.services.dummyproject, calleePkgName: com.something.services.dummyservice.service, action: null
W/ActivityManager: Unable to start service Intent { cmp=com.something.services.dummyservice.service/.DummyService } U=0: not found
DummyService.java文件如下:
package com.something.services.dummyservice.services;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.something.services.dummyservice.IDummyService;
public class DummyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IDummyService.Stub mBinder = new IDummyService.Stub() {
@Override
public String dummyHello() throws RemoteException {
return "Hello";
}
@Override
public void exit() throws RemoteException {
stopSelf();
}
};
}
DummyService清单如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.something.services.dummyservice">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name=".services.DummyService"
android:exported="true"
android:label="@string/app_name"
tools:ignore="ExportedService" />
</application>
</manifest>
IDummyService.aidl文件如下:
// IDummyService.aidl
package com.something.services.dummyservice;
// Declare any non-default types here with import statements
interface IDummyService {
String dummyHello();
void exit();
}
我正在使用上面的代码生成.aar文件。
然后我将该库包含到我的主项目文件中。
我的主项目中的清单如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.something.services.dummyproject">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java文件如下:
package com.something.services.dummyproject;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.something.services.dummyservice.IDummyService;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
private IDummyService mDummyService;
@Bind(R.id.btnStartService)
Button btnStartService;
@Bind(R.id.btnEndService)
Button btnEndService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
btnStartService.setOnClickListener(startServiceOnClickListener);
btnEndService.setOnClickListener(endServiceOnClickListener);
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
mDummyService = IDummyService.Stub.asInterface(service);
dummyHello();
}
@Override
public void onServiceDisconnected(ComponentName className) {
mDummyService = null;
}
};
private final View.OnClickListener startServiceOnClickListener = new View.OnClickListener() {
@Override
public void onClick(final View completeButton) {
Intent intent = new Intent(MainActivity.this, com.something.services.dummyservice.IDummyService.class);
intent.setAction(IDummyService.class.getName());
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
};
private final View.OnClickListener endServiceOnClickListener = new View.OnClickListener() {
@Override
public void onClick(final View completeButton) {
exitService();
}
};
private void dummyHello() {
try {
String response = mDummyService.dummyHello();
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG);
} catch (RemoteException e) {
e.printStackTrace();
}
}
private void exitService() {
try {
if (mDummyService != null) {
mDummyService.exit();
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
dummyProject也包含完全相同的AIDL文件。在这两种情况下,AIDL文件都位于一个sail \ JAVA文件夹旁边的aidl文件夹中。其中包含一个包含AIDL文件的包文件夹。
非常感谢您确定我为什么无法连接到该服务的帮助!