Last active
August 29, 2015 14:05
-
-
Save Proper-Job/56d22b36714e3339be05 to your computer and use it in GitHub Desktop.
Activity call to startService() effectively cancels prior Service call to stopSelf()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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