美文网首页
(Android)AIDL示例

(Android)AIDL示例

作者: 西厌西厌 | 来源:发表于2020-09-22 16: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);
        }
    }

相关文章

网友评论

      本文标题:(Android)AIDL示例

      本文链接:https://www.haomeiwen.com/subject/dcwwyktx.html