Fix service samples, especially remote services.

Change-Id: I02c761f24ffddc1108b2c032f1820eac19ed2c05
This commit is contained in:
Dianne Hackborn
2015-05-13 15:26:07 -07:00
parent 83529e3a2c
commit 57f208bd11
3 changed files with 33 additions and 34 deletions

View File

@@ -38,6 +38,7 @@
<uses-permission android:name="android.permission.TRANSMIT_IR" /> <uses-permission android:name="android.permission.TRANSMIT_IR" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<!-- For android.media.audiofx.Visualizer --> <!-- For android.media.audiofx.Visualizer -->
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.RECORD_AUDIO" />
@@ -550,17 +551,7 @@
</intent-filter> </intent-filter>
</activity> </activity>
<service android:name=".app.RemoteService" android:process=":remote"> <service android:name=".app.RemoteService" android:process=":remote" />
<intent-filter>
<!-- These are the interfaces supported by the service, which
you can bind to. -->
<action android:name="com.example.android.apis.app.IRemoteService" />
<action android:name="com.example.android.apis.app.ISecondary" />
<!-- This is an action code you can use to select the service
without explicitly supplying the implementation class. -->
<action android:name="com.example.android.apis.app.REMOTE_SERVICE" />
</intent-filter>
</service>
<activity android:name=".app.RemoteService$Controller" <activity android:name=".app.RemoteService$Controller"
android:label="@string/activity_remote_service_controller" android:label="@string/activity_remote_service_controller"

View File

@@ -23,6 +23,7 @@ import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.os.Binder; import android.os.Binder;
import android.os.IBinder; import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
@@ -71,9 +72,7 @@ public class LocalService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent); Log.i("LocalService", "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly return START_NOT_STICKY;
// stopped, so return sticky.
return START_STICKY;
} }
@Override @Override

View File

@@ -32,6 +32,7 @@ import android.os.IBinder;
import android.os.Message; import android.os.Message;
import android.os.Process; import android.os.Process;
import android.os.RemoteCallbackList; import android.os.RemoteCallbackList;
import android.util.Log;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.Button; import android.widget.Button;
@@ -79,6 +80,12 @@ public class RemoteService extends Service {
mHandler.sendEmptyMessage(REPORT_MSG); mHandler.sendEmptyMessage(REPORT_MSG);
} }
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
return START_NOT_STICKY;
}
@Override @Override
public void onDestroy() { public void onDestroy() {
// Cancel the persistent notification. // Cancel the persistent notification.
@@ -232,8 +239,7 @@ public class RemoteService extends Service {
// We use an action code here, instead of explictly supplying // We use an action code here, instead of explictly supplying
// the component name, so that other packages can replace // the component name, so that other packages can replace
// the service. // the service.
startService(new Intent( startService(new Intent(Controller.this, RemoteService.class));
"com.example.android.apis.app.REMOTE_SERVICE"));
} }
}; };
@@ -242,8 +248,7 @@ public class RemoteService extends Service {
// Cancel a previous call to startService(). Note that the // Cancel a previous call to startService(). Note that the
// service will not actually stop at this point if there are // service will not actually stop at this point if there are
// still bound clients. // still bound clients.
stopService(new Intent( stopService(new Intent(Controller.this, RemoteService.class));
"com.example.android.apis.app.REMOTE_SERVICE"));
} }
}; };
} }
@@ -361,10 +366,11 @@ public class RemoteService extends Service {
// by interface names. This allows other applications to be // by interface names. This allows other applications to be
// installed that replace the remote service by implementing // installed that replace the remote service by implementing
// the same interface. // the same interface.
bindService(new Intent(IRemoteService.class.getName()), Intent intent = new Intent(Binding.this, RemoteService.class);
mConnection, Context.BIND_AUTO_CREATE); intent.setAction(IRemoteService.class.getName());
bindService(new Intent(ISecondary.class.getName()), bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
mSecondaryConnection, Context.BIND_AUTO_CREATE); intent.setAction(ISecondary.class.getName());
bindService(intent, mSecondaryConnection, Context.BIND_AUTO_CREATE);
mIsBound = true; mIsBound = true;
mCallbackText.setText("Binding."); mCallbackText.setText("Binding.");
} }
@@ -471,6 +477,7 @@ public class RemoteService extends Service {
public static class BindingOptions extends Activity { public static class BindingOptions extends Activity {
ServiceConnection mCurConnection; ServiceConnection mCurConnection;
TextView mCallbackText; TextView mCallbackText;
Intent mBindIntent;
class MyServiceConnection implements ServiceConnection { class MyServiceConnection implements ServiceConnection {
final boolean mUnbindOnDisconnect; final boolean mUnbindOnDisconnect;
@@ -539,6 +546,9 @@ public class RemoteService extends Service {
mCallbackText = (TextView)findViewById(R.id.callback); mCallbackText = (TextView)findViewById(R.id.callback);
mCallbackText.setText("Not attached."); mCallbackText.setText("Not attached.");
mBindIntent = new Intent(this, RemoteService.class);
mBindIntent.setAction(IRemoteService.class.getName());
} }
private OnClickListener mBindNormalListener = new OnClickListener() { private OnClickListener mBindNormalListener = new OnClickListener() {
@@ -548,8 +558,7 @@ public class RemoteService extends Service {
mCurConnection = null; mCurConnection = null;
} }
ServiceConnection conn = new MyServiceConnection(); ServiceConnection conn = new MyServiceConnection();
if (bindService(new Intent(IRemoteService.class.getName()), if (bindService(mBindIntent, conn, Context.BIND_AUTO_CREATE)) {
conn, Context.BIND_AUTO_CREATE)) {
mCurConnection = conn; mCurConnection = conn;
} }
} }
@@ -562,8 +571,8 @@ public class RemoteService extends Service {
mCurConnection = null; mCurConnection = null;
} }
ServiceConnection conn = new MyServiceConnection(); ServiceConnection conn = new MyServiceConnection();
if (bindService(new Intent(IRemoteService.class.getName()), if (bindService(mBindIntent, conn,
conn, Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND)) { Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND)) {
mCurConnection = conn; mCurConnection = conn;
} }
} }
@@ -590,8 +599,8 @@ public class RemoteService extends Service {
mCurConnection = null; mCurConnection = null;
} }
ServiceConnection conn = new MyServiceConnection(); ServiceConnection conn = new MyServiceConnection();
if (bindService(new Intent(IRemoteService.class.getName()), if (bindService(mBindIntent, conn,
conn, Context.BIND_AUTO_CREATE | Context.BIND_ALLOW_OOM_MANAGEMENT)) { Context.BIND_AUTO_CREATE | Context.BIND_ALLOW_OOM_MANAGEMENT)) {
mCurConnection = conn; mCurConnection = conn;
} }
} }
@@ -604,8 +613,8 @@ public class RemoteService extends Service {
mCurConnection = null; mCurConnection = null;
} }
ServiceConnection conn = new MyServiceConnection(true); ServiceConnection conn = new MyServiceConnection(true);
if (bindService(new Intent(IRemoteService.class.getName()), if (bindService(mBindIntent, conn,
conn, Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY)) { Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY)) {
mCurConnection = conn; mCurConnection = conn;
} }
} }
@@ -618,8 +627,8 @@ public class RemoteService extends Service {
mCurConnection = null; mCurConnection = null;
} }
ServiceConnection conn = new MyServiceConnection(); ServiceConnection conn = new MyServiceConnection();
if (bindService(new Intent(IRemoteService.class.getName()), if (bindService(mBindIntent, conn,
conn, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT)) { Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT)) {
mCurConnection = conn; mCurConnection = conn;
} }
} }
@@ -632,8 +641,8 @@ public class RemoteService extends Service {
mCurConnection = null; mCurConnection = null;
} }
ServiceConnection conn = new MyServiceConnection(); ServiceConnection conn = new MyServiceConnection();
if (bindService(new Intent(IRemoteService.class.getName()), if (bindService(mBindIntent, conn,
conn, Context.BIND_AUTO_CREATE | Context.BIND_ADJUST_WITH_ACTIVITY Context.BIND_AUTO_CREATE | Context.BIND_ADJUST_WITH_ACTIVITY
| Context.BIND_WAIVE_PRIORITY)) { | Context.BIND_WAIVE_PRIORITY)) {
mCurConnection = conn; mCurConnection = conn;
} }