Android 多进程开发 - FileDescriptor、Uri、AIDL 接口定义不能抛出异常
FileDescriptor1、AIDLIMyAidlInterface.aidl这里是位于src/main/java/com/my/common包下packagecom.my.common;importandroid.os.ParcelFileDescriptor;interfaceIMyAidlInterface{ParcelFileDescriptorgetFileDescriptor();voidsetFileDescriptor(inParcelFileDescriptorparcelFileDescriptor);}2、ServerprivatefinalIMyAidlInterface.StubbindernewIMyAidlInterface.Stub(){OverridepublicParcelFileDescriptorgetFileDescriptor()throwsRemoteException{FilefilesDirgetFilesDir();FilefilenewFile(filesDir,shared_file.dat);// 如果示例文件不存在则创建并写入一些数据if(!file.exists()){try{file.createNewFile();}catch(IOExceptione){e.printStackTrace();}try{FileOutputStreamfileOutputStreamnewFileOutputStream(file);fileOutputStream.write(server content.getBytes());fileOutputStream.close();}catch(IOExceptione){e.printStackTrace();}}try{returnParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY);}catch(FileNotFoundExceptione){e.printStackTrace();returnnull;}}OverridepublicvoidsetFileDescriptor(ParcelFileDescriptorparcelFileDescriptor)throwsRemoteException{if(parcelFileDescriptor!null){try(FileInputStreamfileInputStreamnewFileInputStream(parcelFileDescriptor.getFileDescriptor())){byte[]contentnewbyte[fileInputStream.available()];intreadfileInputStream.read(content);Log.i(TAG,read read bytes from file);Log.i(TAG,file content: newString(content));}catch(IOExceptione){Log.i(TAG,open file error: e.getMessage());}finally{try{parcelFileDescriptor.close();}catch(IOExceptione){e.printStackTrace();}}}}};3、Client调用 getFileDescriptor 方法try{ParcelFileDescriptorparcelFileDescriptormyAidlInterface.getFileDescriptor();Log.i(TAG,getFileDescriptor method success);if(parcelFileDescriptor!null){try(FileInputStreamfileInputStreamnewFileInputStream(parcelFileDescriptor.getFileDescriptor())){byte[]contentnewbyte[fileInputStream.available()];intreadfileInputStream.read(content);Log.i(TAG,read read bytes from file);Log.i(TAG,file content: newString(content));}catch(IOExceptione){Log.i(TAG,open file error: e.getMessage());}finally{try{parcelFileDescriptor.close();}catch(IOExceptione){e.printStackTrace();}}}}catch(RemoteExceptione){e.printStackTrace();Log.e(TAG,getFileDescriptor method error: e.getMessage());}调用 setFileDescriptor 方法FilefilesDirgetFilesDir();FilefilenewFile(filesDir,shared_file.dat);// 如果示例文件不存在则创建并写入一些数据if(!file.exists()){try{file.createNewFile();}catch(IOExceptione){e.printStackTrace();}try{FileOutputStreamfileOutputStreamnewFileOutputStream(file);fileOutputStream.write(client content.getBytes());fileOutputStream.close();}catch(IOExceptione){e.printStackTrace();}}try{ParcelFileDescriptorparcelFileDescriptorParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY);try{myAidlInterface.setFileDescriptor(parcelFileDescriptor);Log.i(TAG,setFileDescriptor method success);}catch(RemoteExceptione){e.printStackTrace();Log.e(TAG,setFileDescriptor method error: e.getMessage());}}catch(FileNotFoundExceptione){e.printStackTrace();}4、Test调用 getFileDescriptor 方法输出结果如下getFileDescriptor method success read 14 bytes from file file content: server content调用 setFileDescriptor 方法输出结果如下read 14 bytes from file file content: client content setFileDescriptor method successUri1、AIDLIMyAidlInterface.aidl这里是位于src/main/java/com/my/common包下packagecom.my.common;importandroid.net.Uri;interfaceIMyAidlInterface{UrigetUri();voidsetUri(inUriuri);}2、ServerprivatefinalIMyAidlInterface.StubbindernewIMyAidlInterface.Stub(){OverridepublicUrigetUri()throwsRemoteException{FilefilesDirgetFilesDir();FilefilenewFile(filesDir,shared_file.dat);// 如果示例文件不存在则创建并写入一些数据if(!file.exists()){try{file.createNewFile();}catch(IOExceptione){e.printStackTrace();}try{FileOutputStreamfileOutputStreamnewFileOutputStream(file);fileOutputStream.write(server content.getBytes());fileOutputStream.close();}catch(IOExceptione){e.printStackTrace();}}UriuriFileProvider.getUriForFile(ServerService.this,getApplicationContext().getPackageName().fileprovider,file);// 授予临时权限// 第一个参数是客户端应用包名grantUriPermission(com.my.client,uri,Intent.FLAG_GRANT_READ_URI_PERMISSION);returnuri;}OverridepublicvoidsetUri(Uriuri)throwsRemoteException{if(uri!null){try(InputStreaminputStreamgetContentResolver().openInputStream(uri)){byte[]contentnewbyte[inputStream.available()];intreadinputStream.read(content);Log.i(TAG,read read bytes from uri);Log.i(TAG,uri content: newString(content));}catch(IOExceptione){e.printStackTrace();}}}};3、Client调用 getUri 方法try{UriurimyAidlInterface.getUri();Log.i(TAG,getUri method success);if(uri!null){try(InputStreaminputStreamgetContentResolver().openInputStream(uri)){byte[]contentnewbyte[inputStream.available()];intreadinputStream.read(content);Log.i(TAG,read read bytes from uri);Log.i(TAG,uri content: newString(content));}catch(IOExceptione){e.printStackTrace();}}}catch(RemoteExceptione){e.printStackTrace();Log.e(TAG,getUri method error: e.getMessage());}调用 setUri 方法FilefilesDirgetFilesDir();FilefilenewFile(filesDir,shared_file.dat);// 如果示例文件不存在则创建并写入一些数据if(!file.exists()){try{file.createNewFile();}catch(IOExceptione){e.printStackTrace();}try{FileOutputStreamfileOutputStreamnewFileOutputStream(file);fileOutputStream.write(client content.getBytes());fileOutputStream.close();}catch(IOExceptione){e.printStackTrace();}}UriuriFileProvider.getUriForFile(this,getPackageName().fileprovider,file);grantUriPermission(com.my.server,uri,Intent.FLAG_GRANT_READ_URI_PERMISSION);try{myAidlInterface.setUri(uri);Log.i(TAG,setUri method success);}catch(RemoteExceptione){e.printStackTrace();Log.e(TAG,setUri method error: e.getMessage());}4、Test调用 getUri 方法输出结果如下getUri method success read 14 bytes from uri uri content: server content调用 setUri 方法输出结果如下read 14 bytes from uri uri content: client content setUri method successAIDL 接口定义不能抛出异常在 AIDL 接口定义中方法签名本身不能包含 throws 关键字来声明抛出异常会导致编译错误intadd(inta,intb)throwsRemoteException;# 输出结果 syntax error, unexpected identifier, expecting or ;onewayvoiddoSomething()throwsRemoteException;# 输出结果 syntax error, unexpected identifier, expecting or ;将光标移动到 throws 关键字上会出现如下提示信息AidlTokenTypes. ASSIGN expected, got throws
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2418663.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!