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.READ_PHONE_STATE"/>
<!-- For android.media.audiofx.Visualizer -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
@@ -550,17 +551,7 @@
</intent-filter>
</activity>
<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>
<service android:name=".app.RemoteService" android:process=":remote" />
<activity android:name=".app.RemoteService$Controller"
android:label="@string/activity_remote_service_controller"

View File

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

View File

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