返回顶部

收藏

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. Trinea 发表 2016-12-19 10:52:15 滴滴国际化项目 Android 端演进
  2. 草依山 发表 2013-12-13 08:00:00 手工下载Android的SDK
  3. 7 发表 2014-10-18 08:54:12 Android L SDK更新
  4. ReSur 发表 2014-10-23 13:28:57 Safari扩展获取安装包(最新版本)的方法
  5. admin 发表 2018-02-25 03:46:02 Android Wear SDK 和模拟器更新
  6. ANIL K. 发表 2018-05-03 09:31:10 Android 4.0 SDK: Crash during stream subscription
  7. 南 靖男 发表 2018-07-06 02:35:08 flutter 免 Android Studio 安装 Android SDK
  8. fatkun 发表 2013-03-10 05:29:01 安装android sdk提示找不到JDK解决方法
  9. rain 发表 2016-03-10 02:46:26 意外:Android N preview 发布了
  10. Trinea 发表 2016-12-11 13:47:22 三方桌面支持 Android SDK 7.1 新特性 Shortcuts
  11. CppLive 发表 2014-12-24 07:27:26 Mac下部署Android开发环境附加NDK
  12. admin 发表 2018-02-25 03:46:02 Android Wear SDK 和模拟器更新

发表评论