diff --git a/samples/ApiDemos/src/com/example/android/apis/app/Animation.java b/samples/ApiDemos/src/com/example/android/apis/app/Animation.java index 5ba41c497..bd2bd89b8 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/Animation.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/Animation.java @@ -30,10 +30,7 @@ import android.widget.Button; /** - *
Example of explicitly starting and stopping the {@link LocalService}. - * 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 - * as desired.
+ *Example of using a custom animation when transitioning between activities.
*/ public class Animation extends Activity { @Override diff --git a/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java b/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java index f1513ac68..9f84be15a 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java @@ -49,17 +49,33 @@ public class ForegroundService extends Service { static final String ACTION_BACKGROUND = "com.example.android.apis.BACKGROUND"; // BEGIN_INCLUDE(foreground_compatibility) + private static final Class>[] mSetForegroundSignature = new Class[] { + boolean.class}; private static final Class>[] mStartForegroundSignature = new Class[] { int.class, Notification.class}; private static final Class>[] mStopForegroundSignature = new Class[] { boolean.class}; private NotificationManager mNM; + private Method mSetForeground; private Method mStartForeground; private Method mStopForeground; + private Object[] mSetForegroundArgs = new Object[1]; private Object[] mStartForegroundArgs = new Object[2]; private Object[] mStopForegroundArgs = new Object[1]; + void invokeMethod(Method method, Object[] args) { + try { + mStartForeground.invoke(this, mStartForegroundArgs); + } catch (InvocationTargetException e) { + // Should not happen. + Log.w("ApiDemos", "Unable to invoke method", e); + } catch (IllegalAccessException e) { + // Should not happen. + Log.w("ApiDemos", "Unable to invoke method", e); + } + } + /** * This is a wrapper around the new startForeground method, using the older * APIs if it is not available. @@ -69,20 +85,13 @@ public class ForegroundService extends Service { if (mStartForeground != null) { mStartForegroundArgs[0] = Integer.valueOf(id); mStartForegroundArgs[1] = notification; - try { - mStartForeground.invoke(this, mStartForegroundArgs); - } catch (InvocationTargetException e) { - // Should not happen. - Log.w("ApiDemos", "Unable to invoke startForeground", e); - } catch (IllegalAccessException e) { - // Should not happen. - Log.w("ApiDemos", "Unable to invoke startForeground", e); - } + invokeMethod(mStartForeground, mStartForegroundArgs); return; } // Fall back on the old API. - //setForeground(true); + mSetForegroundArgs[0] = Boolean.TRUE; + invokeMethod(mSetForeground, mSetForegroundArgs); mNM.notify(id, notification); } @@ -109,7 +118,8 @@ public class ForegroundService extends Service { // Fall back on the old API. Note to cancel BEFORE changing the // foreground state, since we could be killed at that point. mNM.cancel(id); - //setForeground(false); + mSetForegroundArgs[0] = Boolean.FALSE; + invokeMethod(mSetForeground, mSetForegroundArgs); } @Override @@ -123,6 +133,14 @@ public class ForegroundService extends Service { } catch (NoSuchMethodException e) { // Running on an older platform. mStartForeground = mStopForeground = null; + return; + } + try { + mSetForeground = getClass().getMethod("setForeground", + mSetForegroundSignature); + } catch (NoSuchMethodException e) { + throw new IllegalStateException( + "OS doesn't have Service.startForeground OR Service.setForeground!"); } } diff --git a/samples/ApiDemos/src/com/example/android/apis/app/FragmentAlertDialog.java b/samples/ApiDemos/src/com/example/android/apis/app/FragmentAlertDialog.java index abf9731d8..56ddc6b91 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/FragmentAlertDialog.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/FragmentAlertDialog.java @@ -30,6 +30,9 @@ import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; +/** + * Demonstrates how to show an AlertDialog that is managed by a Fragment. + */ public class FragmentAlertDialog extends Activity { @Override diff --git a/samples/ApiDemos/src/com/example/android/apis/app/_index.html b/samples/ApiDemos/src/com/example/android/apis/app/_index.html index fff5ce2e7..6fe2d4c3a 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/_index.html +++ b/samples/ApiDemos/src/com/example/android/apis/app/_index.html @@ -37,47 +37,131 @@android:process=":remote" to the service in the
AndroidManifest.xml file. Shows how those clients can either start/stop it
- with {@link android.content.Context#startService
- Context.startService} and {@link android.content.Context#stopService
- Context.stopService}, or bind and call it with
- {@link android.content.Context#bindService Context.bindService} and
- {@link android.content.Context#unbindService Context.unindService}.
+ with Context.startService and Context.stopService, or bind and call it with
+ Context.bindService and Context.unindService.
Binding is similar to the local service sample,
but illustrates the additional work (defining aidl
interfaces) needed to interact with a service in another process. Also
shows how a service can publish multiple interfaces and implement
callbacks to its clients.