Merge "Add use of PendingIntent.getForegroundService()" into pi-dev

This commit is contained in:
TreeHugger Robot
2018-03-21 19:45:59 +00:00
committed by Android (Google) Code Review
3 changed files with 31 additions and 0 deletions

View File

@@ -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">

View File

@@ -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

View File

@@ -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,