Merge "Clarified conditions that require client-side unbindService() call." into oc-mr1-dev-plus-aosp
This commit is contained in:
committed by
Android (Google) Code Review
commit
d1cd393c57
@@ -23,6 +23,7 @@ import android.content.ComponentName;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.ServiceConnection;
|
import android.content.ServiceConnection;
|
||||||
|
import android.util.Log;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@@ -36,7 +37,7 @@ public class LocalServiceActivities {
|
|||||||
* This demonstrates the implementation of a service that runs in the same
|
* This demonstrates the implementation of a service that runs in the same
|
||||||
* process as the rest of the application, which is explicitly started and stopped
|
* process as the rest of the application, which is explicitly started and stopped
|
||||||
* as desired.</p>
|
* as desired.</p>
|
||||||
*
|
*
|
||||||
* <p>Note that this is implemented as an inner class only keep the sample
|
* <p>Note that this is implemented as an inner class only keep the sample
|
||||||
* all together; typically this code would appear in some separate class.
|
* all together; typically this code would appear in some separate class.
|
||||||
*/
|
*/
|
||||||
@@ -84,15 +85,20 @@ public class LocalServiceActivities {
|
|||||||
* Example of binding and unbinding to the local service.
|
* Example of binding and unbinding to the local service.
|
||||||
* This demonstrates the implementation of a service which the client will
|
* This demonstrates the implementation of a service which the client will
|
||||||
* bind to, receiving an object through which it can communicate with the service.
|
* bind to, receiving an object through which it can communicate with the service.
|
||||||
*
|
*
|
||||||
* Note that this is implemented as an inner class only keep the sample
|
* Note that this is implemented as an inner class only keep the sample
|
||||||
* all together; typically this code would appear in some separate class.
|
* all together; typically this code would appear in some separate class.
|
||||||
*/
|
*/
|
||||||
public static class Binding extends Activity {
|
public static class Binding extends Activity {
|
||||||
private boolean mIsBound;
|
// BEGIN_INCLUDE(bind)
|
||||||
|
// Don't attempt to unbind from the service unless the client has received some
|
||||||
|
// information about the service's state.
|
||||||
|
private boolean mShouldUnbind;
|
||||||
|
|
||||||
|
// To invoke the bound service, first make sure that this value
|
||||||
|
// is not null.
|
||||||
private LocalService mBoundService;
|
private LocalService mBoundService;
|
||||||
|
|
||||||
private ServiceConnection mConnection = new ServiceConnection() {
|
private ServiceConnection mConnection = new ServiceConnection() {
|
||||||
public void onServiceConnected(ComponentName className, IBinder service) {
|
public void onServiceConnected(ComponentName className, IBinder service) {
|
||||||
// This is called when the connection with the service has been
|
// This is called when the connection with the service has been
|
||||||
@@ -101,7 +107,7 @@ public class LocalServiceActivities {
|
|||||||
// service that we know is running in our own process, we can
|
// service that we know is running in our own process, we can
|
||||||
// cast its IBinder to a concrete class and directly access it.
|
// cast its IBinder to a concrete class and directly access it.
|
||||||
mBoundService = ((LocalService.LocalBinder)service).getService();
|
mBoundService = ((LocalService.LocalBinder)service).getService();
|
||||||
|
|
||||||
// Tell the user about this for our demo.
|
// Tell the user about this for our demo.
|
||||||
Toast.makeText(Binding.this, R.string.local_service_connected,
|
Toast.makeText(Binding.this, R.string.local_service_connected,
|
||||||
Toast.LENGTH_SHORT).show();
|
Toast.LENGTH_SHORT).show();
|
||||||
@@ -117,25 +123,30 @@ public class LocalServiceActivities {
|
|||||||
Toast.LENGTH_SHORT).show();
|
Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void doBindService() {
|
void doBindService() {
|
||||||
// Establish a connection with the service. We use an explicit
|
// Attempts to establish a connection with the service. We use an
|
||||||
// class name because we want a specific service implementation that
|
// explicit class name because we want a specific service
|
||||||
// we know will be running in our own process (and thus won't be
|
// implementation that we know will be running in our own process
|
||||||
// supporting component replacement by other applications).
|
// (and thus won't be supporting component replacement by other
|
||||||
bindService(new Intent(Binding.this,
|
// applications).
|
||||||
LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
|
if (bindService(new Intent(Binding.this, LocalService.class),
|
||||||
mIsBound = true;
|
mConnection, Context.BIND_AUTO_CREATE)) {
|
||||||
}
|
mShouldUnbind = true;
|
||||||
|
} else {
|
||||||
void doUnbindService() {
|
Log.e("MY_APP_TAG", "Error: The requested service doesn't " +
|
||||||
if (mIsBound) {
|
"exist, or this client isn't allowed access to it.");
|
||||||
// Detach our existing connection.
|
|
||||||
unbindService(mConnection);
|
|
||||||
mIsBound = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void doUnbindService() {
|
||||||
|
if (mShouldUnbind) {
|
||||||
|
// Release information about the service's state.
|
||||||
|
unbindService(mConnection);
|
||||||
|
mShouldUnbind = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
@@ -154,7 +165,7 @@ public class LocalServiceActivities {
|
|||||||
doUnbindService();
|
doUnbindService();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|||||||
Reference in New Issue
Block a user