1

我有一个名为 RemoteServiceConnection() 的依赖项目,通过使用 AIDL,我可以访问 Tabhost 子活动中的方法。

我有一个包含 3 个子活动的 Tabhost

因此,对于 tabhost 的第一个子活动, getApplicationContext.bindService() 工作正常。

实际情况是我正在访问从远程服务连接到第一个子活动的值(比如速度、温度)并在 UI 上显示

对于 tabhost 的第二个子活动,我是否需要重复我在第一个子活动中的相同代码。

我不想为 tabhost 的第二个和第三个子活动重复相同的代码。

我浏览了这些链接,但没有得到任何正确的想法

使用基类活动将多个活动(选项卡)绑定到服务

http://code.google.com/p/android/issues/detail?id=2483

对于第二个子活动(SecondTab.java),我按照上面的链接做了一些更改,但 UI 没有获取数据。

任何帮助总是受到赞赏。谢谢

FirstTab.java(对我来说工作正常)

    public class FirstTab extends Activity {
    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.hvac);

    startService();
    bindService();
    System.gc();

    serviceHandler = new Handler();
    serviceHandler.postDelayed(myTask, 1000L);

}

@Override
public Context getApplicationContext() {
    // TODO Auto-generated method stub
    return super.getApplicationContext();
    // getApplicationContext().bindService(i, conn, flags)
}

class RemoteServiceConnection implements ServiceConnection {

    public void onServiceConnected(ComponentName className,
            IBinder boundService) {
        remoteService = IMyRemoteService.Stub
                .asInterface((IBinder) boundService);
        invokeService();
        Log.d(getClass().getSimpleName(), "onServiceConnected()");
    }

    public void onServiceDisconnected(ComponentName className) {
        remoteService = null;
        // updateServiceStatus();
        // getApplicationContext().unbindService(conn);
        Log.d(getClass().getSimpleName(), "onServiceDisconnected");
    }
};

private void startService() {
    if (started) {
        // Toast.makeText(CarHome.this, "Service already started",
        // Toast.LENGTH_SHORT).show();
    } else {
        Intent i = new Intent();
        i.setClassName("com.msat.home.clusterservices",
                "com.msat.home.clusterservices.RemoteService");
        getApplicationContext().startService(i);
        started = true;
        updateServiceStatus();
        Log.d(getClass().getSimpleName(), "startService()");
    }

}

private void stopService() {
    if (!started) {
        // drivertmpcount.setText(Integer.toString(hvactemp)); //
        // Toast.makeText(CarHome.this, "Service not yet started",
        // Toast.LENGTH_SHORT).show();
    } else {
        Intent i = new Intent();
        i.setClassName("com.msat.home.clusterservices",
                "com.msat.home.clusterservices.RemoteService");
        getApplicationContext().stopService(i);
        started = false;
        updateServiceStatus();
        Log.d(getClass().getSimpleName(), "stopService()");
    }
}

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

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

private void invokeService() {

    if (conn == null) {
        // Toast.makeText(CarHome.this, "Cannot invoke - service not bound",
        // Toast.LENGTH_SHORT).show();
    } else {
        try {
            System.out.println(remoteService);

            final TextView drivertmpcount = (TextView) findViewById(R.id.curtempcount);
            final TextView destmpcount = (TextView) findViewById(R.id.destempcount);
            // final TextView tempcountpass = (TextView)
            // findViewById(R.id.tempcountpass);

            hvactemp = remoteService.getHvacTemp();
            hvacTemppass = remoteService.getHvacTemppass();
            System.out.println("Raghav hvac" + hvactemp);

            System.out.println("jaydeep speed" + speed);
            // rpm_text.setText(rpm);
            destmpcount.setText(Integer.toString(hvactemp));
            drivertmpcount.setText(Integer.toString(hvactemp));
            // tempcountpass.setText(Integer.toString(hvacTemppass));

            Log.d(getClass().getSimpleName(), "invokeService()");
        } catch (RemoteException re) {
            Log.e(getClass().getSimpleName(), "RemoteException");
        }
    }
}

private void updateServiceStatus() {
    String bindStatus = conn == null ? "unbound" : "bound";
    String startStatus = started ? "started" : "not started";
    String statusText = "Service status: " + bindStatus + "," + startStatus;
    // TextView t = (TextView)findViewById( R.id.serviceStatus);
    // t.setText( statusText );
    System.out.println("Jaydeep : " + statusText);
}

protected void onDestroy() {
    super.onDestroy();
    // getApplicationContext().unbindService(conn);
    releaseService();
    Log.d(getClass().getSimpleName(), "onDestroy()");
}

class Task implements Runnable {
    public void run() {
        // invokeService();
        if (remoteService != null) {
            invokeService(); // getting ERROR here
        } else {
            serviceHandler.postDelayed(this, 1000L);
        }
        // serviceHandler.postDelayed(this, 1000L);
        Log.i(getClass().getSimpleName(),
                "Incrementing engineRPM in the run method");
    }
}
  }

SecondTab.java

         public class SecondTab extends Activity implements ServiceConnection {

         @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hvacpass);

}

public void onServiceConnected(ComponentName className, IBinder boundService) {
    // TODO Auto-generated method stub
    remoteService = IMyRemoteService.Stub
            .asInterface((IBinder) boundService);
    Log.d(getClass().getSimpleName(), "onServiceConnected()");

    Intent i = new Intent();
    i.setClassName("com.msat.home.clusterservices",
            "com.msat.home.clusterservices.RemoteService");
    getApplicationContext().startService(i);
    getApplicationContext().bindService(i, this, Context.BIND_AUTO_CREATE);

    try {
        passcurtempcount = (TextView) findViewById(R.id.passcurtempcount);

        hvactemppass = remoteService.getHvacTemppass();
        passcurtempcount.setText(Integer.toString(hvactemppass));
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

public void onServiceDisconnected(ComponentName className) {
    // TODO Auto-generated method stub
    remoteService = null;

        }
            }
4

0 回答 0