Android:getApplicationcontext.bindService()不适用于Tabhost的第二个子活动

时间:2012-11-30 04:37:34

标签: android user-interface service android-tabhost childactivity

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

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

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

实际上我正在访问从远程服务连接到第一个子活动并在UI上显示的值(比如speed,temp)

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

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

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

Binding Multiple Activities(Tabs) to a Service using a Base Class Activity

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;

        }
            }

0 个答案:

没有答案