(Android)AIDL示例

2020-09-22  本文已影响0人  西厌西厌
  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);
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读