Reputation: 3698
I have a dependency project named RemoteServiceConnection() and through this using AIDL I could able to access the methods inside my child activity of Tabhost.
I have a Tabhost with 3 child activities
So for the first child activity of tabhost, getApplicationContext.bindService() is working fine.
The actual thing is I am accessing the values(say speed,temp) from remote service connection to the First child activity and dispalying on the UI
and for the second child activity of tabhost do i need to repeat the same code which I have in my first child activity.
I dont want to repeat the same code for the 2nd and 3rd child activities of tabhost.
I went through these links but not getting any proper Idea
Binding Multiple Activities(Tabs) to a Service using a Base Class Activity
http://code.google.com/p/android/issues/detail?id=2483
For the second child activity(SecondTab.java) I have done some changes by following the above links but the UI is not getting the data.
Any help is always appreciated. Thanks
FirstTab.java(Its working fine for me)
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;
}
}
Upvotes: 1
Views: 1070