Service使用总结
来源:互联网 发布:电脑培训软件机构咨询 编辑:程序博客网 时间:2024/06/12 01:45
Service使用总结
调用服务中的方法
interface IApp { void install(); void unInstall(); void update();}public class AppManagerService extends Service { @Override public void onCreate() { super.onCreate(); LogUtils.e("AppManagerService onCreate"); } @Nullable @Override public IBinder onBind(Intent intent) { LogUtils.e("AppManagerService onBind"); return new AppManagerBinder(); } private void install() { LogUtils.e("AppManagerService install"); } private void unInstall() { LogUtils.e("AppManagerService unInstall"); } private void update() { LogUtils.e("AppManagerService update"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { int command = super.onStartCommand(intent, flags, startId); LogUtils.e("AppManagerService onStartCommand"); return command; } private class AppManagerBinder extends Binder implements IApp { @Override public void install() { LogUtils.e("AppManagerBinder install"); AppManagerService.this.install(); } @Override public void unInstall() { LogUtils.e("AppManagerBinder unInstall"); AppManagerService.this.unInstall(); } @Override public void update() { LogUtils.e("AppManagerBinder update"); AppManagerService.this.update(); } }}
>
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private AppServiceConn conn; private IApp mAppManagerBinder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addListener(); initData(); } private void initData() { bindLocalService(); } private void bindLocalService() { Intent service = new Intent(this, AppManagerService.class); startService(service); conn = new AppServiceConn(); bindService(service, conn, Service.BIND_AUTO_CREATE); } private void addListener() { findViewById(R.id.btn_bind_service).setOnClickListener(this); findViewById(R.id.btn_call_method).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_bind_service: Intent service = new Intent(this, AppManagerService.class); startService(service); conn = new AppServiceConn(); bindService(service, conn, Service.BIND_AUTO_CREATE); break; case R.id.btn_call_method: if (mAppManagerBinder != null) { mAppManagerBinder.install(); mAppManagerBinder.unInstall(); mAppManagerBinder.update(); } break; } } private class AppServiceConn implements ServiceConnection { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { LogUtils.e("AppServiceConn onServiceConnected"); mAppManagerBinder = (IApp) iBinder; } @Override public void onServiceDisconnected(ComponentName componentName) { LogUtils.e("AppServiceConn onServiceDisconnected"); } }}
调用远程服务
server端
main/aidl
interface IPay { void pay(); void reverse();}
main/java/service
public class PayService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return new PayServiceBinder();}private class PayServiceBinder extends IPay.Stub { @Override public void pay() throws RemoteException { LogUtils.e("PayServiceBinder pay"); doPay(); } @Override public void reverse() throws RemoteException { LogUtils.e("PayServiceBinder reverse"); doRerverse(); }}private void doRerverse() { LogUtils.e("PayService doRerverse");}private void doPay() { LogUtils.e("PayService doPay");}
}
清单文件中的配置
<service android:name=".PayService"> <intent-filter> <action android:name="com.example.bindserviceaidl.XXXX"/> </intent-filter></service>
client端
将main/aidl中的aidl目录直接全部复制到客户端的main中
调用如下
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ClientServiceConn mConn; private IPay mIBinder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addListener(); bindRemoteService(); } private void addListener() { findViewById(R.id.tv).setOnClickListener(this); } private void bindRemoteService() { Intent service = new Intent(); service.setAction("com.example.bindserviceaidl.XXXX"); mConn = new ClientServiceConn(); bindService(service, mConn, Service.BIND_AUTO_CREATE); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.tv: if (mIBinder != null) { try { mIBinder.pay(); mIBinder.reverse(); } catch (RemoteException e) { e.printStackTrace(); } } break; } } private class ClientServiceConn implements ServiceConnection { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { LogUtils.e("ClientServiceConn onServiceConnected"); // 这个是远程使用的方式,本地使用可以强制转换 mIBinder = IPay.Stub.asInterface(iBinder); } @Override public void onServiceDisconnected(ComponentName componentName) { LogUtils.e("ClientServiceConn onServiceDisconnected"); } }}
阅读全文
0 0
- Android Service 使用总结
- Service使用总结
- Service使用总结
- Java Service Wrapper使用总结
- Service总结及使用注意事项
- Android服务Service使用总结
- Android服务Service使用总结
- SQL Reporting Service使用代码总结
- android之service组件使用总结
- service 总结
- Service总结
- Service总结
- service总结
- Service总结
- Service总结
- Service总结
- service总结
- Service总结
- angularjs方式的get与post分析
- MSP430G2xx1系列USI的SPI模式详解
- flask数据用ECharts图表形式展现
- ViewPager和ViewPager+Fragment简单使用
- mysql事务的理解
- Service使用总结
- Go编程基础—并发(concurrency)
- python opencv 资料收集
- Device eth0 does not seem to be present,delaying initialization的解决办法
- CRC检验解析
- Flume整合mysql碰到的自定义source表名问题
- stm32菜鸟自学代码
- 开机第二屏幕分辨率改变的几种解决方法
- java 中 return 语句总结