(Android)AIDL示例

  1. 在服务端创建android.content.pm包;在该包内创建IRemote.aidl类
interface IRemote{

   //开始下载
   boolean startDownload(String path,int downloadState ,int downloadIndex);
   //应用卸载
   boolean uninstallApp(String pkg);
}

2.xml定义

 <service 
    android:name="com.mozillaonline.providers.downloads.DownloadRemoteService" 
    android:process=":remote" >
        <intent-filter android:priority="1000">
            <action android:name="com.mozillaonline.providers.downloads.DownloadRemoteService" />
        </intent-filter>
</service>

3.创建类

public class DownloadRemoteService extends Service{
    
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return mBinder;
    }
    
    private final IRemote.Stub mBinder = new Stub(){
        @Override
        public boolean startDownload(final String path, final int paused, final int downloadIndex) throws RemoteException {
            DownloadRemoteUtil.startDownload(path,paused,downloadIndex);
            return true;
        }

        @Override
        public boolean uninstallApp(String pkg) throws RemoteException {            
            return UninstallUtils.uninstallApp(pkg);
        }
        
    };
}

4.应用端将服务端生成的gen文件Iremote.java移过来,必须在android.content.pm包下

5.实例化Service

    private void initServiceConnection() {
        mServiceConnection = new ServiceConnection() {
            
            @Override
            public void onServiceDisconnected(ComponentName name) {
                mIremote = null;
                initServiceConnection();
            }
            
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                mIremote = IRemote.Stub.asInterface(service);
            }
        };
        if(null == mIremote){
            Intent intent = new Intent();
            intent.setAction("com.mozillaonline.providers.downloads.DownloadRemoteService");
            bindService(intent, mServiceConnection, Service.BIND_AUTO_CREATE);
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容