summaryrefslogtreecommitdiffstats
path: root/samples/ApiDemos
diff options
context:
space:
mode:
authorChristopher Tate <ctate@google.com>2018-03-19 16:25:01 -0700
committerChristopher Tate <ctate@google.com>2018-03-19 16:25:01 -0700
commitfc7c9f5d5cde1cb109e52fbd723d7ed144b2cffa (patch)
tree6776b35fcb1d7dad50fc1ede18de5c821c021e95 /samples/ApiDemos
parent135989335bc89153dbd365c6fa03600f0304103a (diff)
downloadandroid_development-fc7c9f5d5cde1cb109e52fbd723d7ed144b2cffa.tar.gz
android_development-fc7c9f5d5cde1cb109e52fbd723d7ed144b2cffa.tar.bz2
android_development-fc7c9f5d5cde1cb109e52fbd723d7ed144b2cffa.zip
Add use of PendingIntent.getForegroundService()
In particular, we have an example of a foreground-service PendingIntent triggered by a 15-second delayed alarm following a button click. This is enough time to exercise use cases like swiping the app out of recents before the alarm fires, therefore entering the "starting a fg service when the app is not in a fg state" flows. Bug: 73559697 Test: this is it Change-Id: If57908ab852c7a0bfab7bb1da8eda0a47493fc6b
Diffstat (limited to 'samples/ApiDemos')
-rw-r--r--samples/ApiDemos/res/layout/foreground_service_controller.xml6
-rw-r--r--samples/ApiDemos/res/values/strings.xml1
-rw-r--r--samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java24
3 files changed, 31 insertions, 0 deletions
diff --git a/samples/ApiDemos/res/layout/foreground_service_controller.xml b/samples/ApiDemos/res/layout/foreground_service_controller.xml
index f2a62741a..bf15fef07 100644
--- a/samples/ApiDemos/res/layout/foreground_service_controller.xml
+++ b/samples/ApiDemos/res/layout/foreground_service_controller.xml
@@ -60,6 +60,12 @@
android:text="@string/start_service_foreground_2">
</Button>
+ <Button android:id="@+id/start_foreground_2_alarm"
+ android:layout_width="wrap_content" android:layout_height="wrap_content"
+ android:layout_marginTop="8dp"
+ android:text="@string/start_service_foreground_2_alarm">
+ </Button>
+
<Button android:id="@+id/stop_2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/stop_service_2">
diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml
index 59f2fe96e..4717a40cf 100644
--- a/samples/ApiDemos/res/values/strings.xml
+++ b/samples/ApiDemos/res/values/strings.xml
@@ -332,6 +332,7 @@
<string name="start_service_background">Start Service Background</string>
<string name="start_service_background_wakelock">Start Service Background w/Wakelock</string>
<string name="start_service_foreground_2">Start Service Foreground 2</string>
+ <string name="start_service_foreground_2_alarm">Start Service Foreground 2 Via Alarm</string>
<string name="activity_isolated_service_controller">App/Service/Isolated Service Controller</string>
<string name="isolated_service_controller">This demonstrates the use of android:isolatedProcess
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 8d34b0c9f..bc1b46f0f 100644
--- a/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java
+++ b/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java
@@ -17,15 +17,18 @@
package com.example.android.apis.app;
import android.app.Activity;
+import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
+import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
+import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
@@ -34,6 +37,9 @@ import android.widget.Button;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+
+
+
// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import com.example.android.apis.R;
@@ -168,6 +174,8 @@ public class ForegroundService extends Service {
button.setOnClickListener(mForegroundListener2);
button = (Button)findViewById(R.id.stop_2);
button.setOnClickListener(mStopListener2);
+ button = (Button)findViewById(R.id.start_foreground_2_alarm);
+ button.setOnClickListener(mForegroundAlarmListener);
}
private OnClickListener mForegroundListener = new OnClickListener() {
@@ -217,6 +225,22 @@ public class ForegroundService extends Service {
}
};
+ private OnClickListener mForegroundAlarmListener = new OnClickListener() {
+ public void onClick(View v) {
+ final Context ctx = Controller.this;
+
+ final Intent intent = new Intent(ForegroundService.ACTION_FOREGROUND);
+ intent.setClass(ctx, ForegroundService2.class);
+
+ PendingIntent pi = PendingIntent.getForegroundService(ctx, 0, intent, 0);
+ AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
+ am.setExact(AlarmManager.ELAPSED_REALTIME,
+ SystemClock.elapsedRealtime() + 15_000,
+ pi);
+ Log.i("ForegroundService", "Starting service in 15 seconds");
+ }
+ };
+
private OnClickListener mStopListener2 = new OnClickListener() {
public void onClick(View v) {
stopService(new Intent(Controller.this,