问题
run time exception:
System.err:java.lang.RuntimeException: Stub! at com.google.android.things.pio.PeripheralManager.getInstance(PeripheralManager.java:21) at com.afollestad.remotedemo.RemoteService$1.testPeripheralManager(RemoteService.java:33) at com.afollestad.remotedemo.IRemoteAIDL$Stub.onTransact(IRemoteAIDL.java:56) at android.os.Binder.execTransact(Binder.java:697)
Remote service's Manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.afollestad.remotedemo">
<uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />
<application>
<service
android:name=".RemoteService"
android:process=":remote">
<intent-filter>
<action android:name="service.remote" />
</intent-filter>
</service>
</application>
MainActivity:
public class MainActivity extends Activity {
private IRemoteAIDL mService;
private TextView result;
private Button connectRemote;
private Button disconnectRemote;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
Log.d("aidl", "onServiceConnected" + service + " className " + className);
mService = IRemoteAIDL.Stub.asInterface(service);
try {
mService.testPeripheralManager();
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void onServiceDisconnected(ComponentName className) {
Log.d("aidl", "onServiceDisconnected");
mService = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView) findViewById(R.id.result);
connectRemote = (Button) findViewById(R.id.connect_remote);
disconnectRemote = (Button) findViewById(R.id.disconnect_remote);
connectRemote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("aidl", "onClick new thread");
Intent intent = new Intent();
intent.setAction("service.remote");
intent.setPackage("com.afollestad.remotedemo");
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
});
disconnectRemote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("aidl", "onClick");
unbindService(mConnection);
}
});
}}
remote service:
public class RemoteService extends Service {
private I2cDevice mI2cDevice = null;
private static final String I2C_DEVICE_NAME = "I2C1";
private static final int I2C_ADDRESS = 0x5c;
public Context mContext;
private boolean flag;
@Override
public void onCreate() {
super.onCreate();
}
private final IRemoteAIDL.Stub remoteBinder = new IRemoteAIDL.Stub() {
@Override
public void testPeripheralManager(){
Log.d("aidl", "RemoteService PeripheralManager");
try {
PeripheralManager manager = PeripheralManager.getInstance();
Log.d("aidl", "PeripheralManager2");
mI2cDevice = manager.openI2cDevice(I2C_DEVICE_NAME, I2C_ADDRESS);
if (mI2cDevice != null) Log.i("aidl", "I2C device open successful!");
}catch(RuntimeException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
@Override
public IBinder onBind(Intent intent) {
Log.d("aidl", "onBind");
mContext = this;
return remoteBinder;
}
@Override
public boolean onUnbind(Intent intent) {
flag = true;
return super.onUnbind(intent);
}
I bulid remote service in android things module, call PeripheralManager.getInstance() in remote service.
If any solve this problem, Please help me.
回答1:
You should have a uses-library as part of the Manifest declaration, that would prevent you from installing an apk that requires Android Things in a device that does not have it, which looks like is what is happening here.
来源:https://stackoverflow.com/questions/50603035/remote-service-call-peripheralmanager-getinstance