
package com.zxq.zjctx;interface IMyAidlInterface { void sservice();}2.创建完成之后,build一下,不行就重启一下studio,然后写自己的service文件,在Service类中创建ibander对象并返回 @Override public IBinder onBind(Intent intent) { Log.e("))))->","onBind"); return mIBinder; } PRivate IBinder mIBinder = new IMyAidlInterface.Stub() { @Override public void sservice() throws RemoteException { stopservice();//要调用的方法。 } };给service设置一个独立的进程。<service android:name="service.MyService" android:process="com.zxq.zjctx.dulijincheng" />3. 在启动service的类中执行下面的代码,拿到那个接口,通过接口去掉用Intent intent=new Intent(this,MyService.class); startService(intent); bindService(intent,new MyserviceConnect(), Context.BIND_AUTO_CREATE); public class MyserviceConnect implements ServiceConnection { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);//通过这个对象去掉用方法。 } @Override public void onServiceDisconnected(ComponentName componentName) { } }4.调用传递过来对象ImyAidlInterface,这个接口中自己定义的什么方法就调用什么方法。
新闻热点
疑难解答