Skip to content

Instantly share code, notes, and snippets.

@meeDamian
Created February 17, 2012 15:47
Show Gist options
  • Save meeDamian/1854102 to your computer and use it in GitHub Desktop.
Save meeDamian/1854102 to your computer and use it in GitHub Desktop.
Service in TabActivity - TabActivity
package com.example;
import com.example.app.NewsActivity;
import com.example.app.services.LocalService;
import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TabHost;
public class Local2Activity extends TabActivity {
public LocalService mService;
private boolean mIsBound = false;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected( ComponentName className, IBinder service ) {
mService = ((LocalService.LocalBinder)service).getService();
}
public void onServiceDisconnected(ComponentName className) {
mService = null;
}
};
void doBindService() {
getApplicationContext().bindService(new Intent(Local2Activity.this,LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
void doUnbindService() {
if( mIsBound ) {
unbindService( mConnection );
mIsBound = false;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doBindService();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
getWindow().setFeatureDrawable(Window.FEATURE_LEFT_ICON, res.getDrawable(R.drawable.ic_titlebar));
intent = new Intent().setClass(this, NewsActivity.class);
spec = tabHost.newTabSpec("news")
.setIndicator("News", res.getDrawable(R.drawable.ic_tab_news) )
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
protected void onStart() {
super.onStart();
}
protected void onDestroy() {
super.onDestroy();
doUnbindService();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment