返回顶部

收藏

Android 的 Remote Service 开发实例

更多

RemoteService.zip 服务端RemoteServiceClient.zip 客户端

[Java]代码

private void startService(){
     if (started) {
       Toast.makeText(RemoteServiceClient.this, "Service already started", Toast.LENGTH_SHORT).show();
     } else {
       Intent i = new Intent();
       i.setClassName("com.collabera.labs.sai", "com.collabera.labs.sai.RemoteService");
       startService(i);
       started = true;
       updateServiceStatus();
       Log.d( getClass().getSimpleName(), "startService()" );
      }

  }

private void bindService() {
     if(conn == null) {
        conn = new RemoteServiceConnection();
        Intent i = new Intent();
        i.setClassName("com.collabera.labs.sai", "com.collabera.labs.sai.RemoteService");
        bindService(i, conn, Context.BIND_AUTO_CREATE);
        updateServiceStatus();
        Log.d( getClass().getSimpleName(), "bindService()" );
     } else {
       Toast.makeText(RemoteServiceClient.this, "Cannot bind - service already bound", Toast.LENGTH_SHORT).show();
     }
}

private void invokeService() {
     if(conn == null) {
        Toast.makeText(RemoteServiceClient.this, "Cannot invoke - service not bound", Toast.LENGTH_SHORT).show();
     } else {
        try {
            int counter = remoteService.getCounter();
            TextView t = (TextView)findViewById(R.id.notApplicable);
            t.setText( "Counter value: "+Integer.toString( counter ) );
            Log.d( getClass().getSimpleName(), "invokeService()" );
        } catch (RemoteException re) {
            Log.e( getClass().getSimpleName(), "RemoteException" );
        }
     }
}     

private void releaseService() {
      if(conn != null) {
            unbindService(conn);
            conn = null;
            updateServiceStatus();
            Log.d( getClass().getSimpleName(), "releaseService()" );
      } else {
            Toast.makeText(RemoteServiceClient.this, "Cannot unbind - service not bound", Toast.LENGTH_SHORT).show();
      }
}

private void stopService() {
      if (!started) {
            Toast.makeText(RemoteServiceClient.this, "Service not yet started", Toast.LENGTH_SHORT).show();
      } else {
            Intent i = new Intent();
            i.setClassName("com.collabera.labs.sai", "com.collabera.labs.sai.RemoteService");
            stopService(i);
            started = false;
            updateServiceStatus();
            Log.d( getClass().getSimpleName(), "stopService()" );
      }
}

标签:Android,SDK

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 博主 发表 2014-08-29 15:25:00 android-sdk
  2. 博主 发表 2017-12-27 08:28:37 Android埋点技术分析
  3. K-Res 发表 2016-01-31 07:36:02 iOS Android平台下的wchar_t默认size问题
  4. Javen Fang 发表 2014-02-18 01:00:21 RegistrationID 精确对点推送
  5. admin 发表 2018-02-25 03:46:02 Android Wear SDK 和模拟器更新
  6. 博主 发表 2014-12-22 01:07:22 让AndroidSDK飞
  7. K-Res 发表 2016-11-02 05:59:12 百度鹰眼SDK Android版后台服务的重绑定问题
  8. K-Res 发表 2016-11-07 10:44:52 Android百度地图SDK文字覆盖物的换行和旋转中心点问题
  9. admin 发表 2018-02-25 03:46:02 Android Wear SDK 和模拟器更新
  10. ANIL K. 发表 2018-05-03 09:31:10 Android 4.0 SDK: Crash during stream subscription
  11. Aj 发表 2016-01-05 04:51:29 几种Android模拟器对比
  12. 雨点点 发表 2016-05-07 05:34:17 七牛---关于Android SDK jar包依赖情况总结

发表评论