Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Proper-Job
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Proper-Job/56d22b36714e3339be05 to your computer and use it in GitHub Desktop.
Save Proper-Job/56d22b36714e3339be05 to your computer and use it in GitHub Desktop.
Activity call to startService() effectively cancels prior Service call to stopSelf()
public class BindingActivity extends Activity {
private ServiceConnection boundStartedServiceConnection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.binding_activity);
boundStartedServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
Log.i(BindingActivity.class.getSimpleName(), "Service bound");
}
@Override
public void onServiceDisconnected(ComponentName className) {
Log.i(BindingActivity.class.getSimpleName(), "Service disconnected");
}
};
Button startAndStop = (Button)findViewById(R.id.start_and_stop_service);
startAndStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(BoundStartedService.ACTION_START_AND_STOP_SELF, null, BindingActivity.this, BoundStartedService.class);
startService(intent);
}
});
Button startServiceOnly = (Button)findViewById(R.id.start_service_only);
startServiceOnly.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(BoundStartedService.ACTION_START_ONLY, null, BindingActivity.this, BoundStartedService.class);
startService(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, BoundStartedService.class);
bindService(intent, boundStartedServiceConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
unbindService(boundStartedServiceConnection);
}
}
public class BoundStartedService extends Service {
private static final String TAG = BoundStartedService.class.getSimpleName();
private IBinder serviceBinder;
public static final String ACTION_START_AND_STOP_SELF = "ACTION_START_AND_STOP_SELF";
public static final String ACTION_START_ONLY = "ACTION_START_ONLY";
@Override
public IBinder onBind(Intent intent) {
return serviceBinder;
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Service onCreate()");
serviceBinder = new BoundStartedServiceBinder();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Service onDestroy()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (null != intent && null != intent.getAction()) {
String action = intent.getAction();
if (action.equals(ACTION_START_ONLY)) {
Log.i(TAG, "Service onStartCommand start only");
}else if (action.equals(ACTION_START_AND_STOP_SELF)) {
Log.i(TAG, "Service onStartCommand stop self");
stopSelf();
}
}
return START_STICKY;
}
public class BoundStartedServiceBinder extends Binder {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment