Fix issue #8520788: Add WakefulBroadcastReceiver
Add sample code.
Change-Id: I46f4f25ceb4e12f975e3cebd83f62de2555a8996
diff --git a/samples/Support4Demos/AndroidManifest.xml b/samples/Support4Demos/AndroidManifest.xml
index 8d20c9e..ce33ec1 100644
--- a/samples/Support4Demos/AndroidManifest.xml
+++ b/samples/Support4Demos/AndroidManifest.xml
@@ -23,6 +23,7 @@
package="com.example.android.supportv4">
<uses-permission android:name="android.permission.READ_CONTACTS" />
+ <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="13" />
@@ -242,6 +243,17 @@
<service android:name=".content.LocalServiceBroadcaster$LocalService"
android:stopWithTask="true" />
+ <activity android:name=".content.SimpleWakefulController"
+ android:label="@string/simple_wakeful_controller">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
+ </intent-filter>
+ </activity>
+
+ <receiver android:name=".content.SimpleWakefulReceiver" />
+ <service android:name=".content.SimpleWakefulService" />
+
<activity android:name=".accessibility.AccessibilityManagerSupportActivity"
android:label="@string/accessibility_manager_title">
<intent-filter>
diff --git a/samples/Support4Demos/res/layout/wakeful_alarm_controller.xml b/samples/Support4Demos/res/layout/wakeful_alarm_controller.xml
new file mode 100644
index 0000000..725331d
--- /dev/null
+++ b/samples/Support4Demos/res/layout/wakeful_alarm_controller.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical" android:padding="4dip"
+ android:gravity="center_horizontal"
+ android:layout_width="match_parent" android:layout_height="match_parent">
+
+ <TextView
+ android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:layout_weight="0" android:paddingBottom="4dip"
+ android:textAppearance="?android:attr/textAppearanceMedium"
+ android:text="@string/simple_wakeful_controller_msg"/>
+
+ <Button android:id="@+id/schedule"
+ android:layout_width="wrap_content" android:layout_height="wrap_content"
+ android:text="@string/schedule_wakeful_alarm">
+ <requestFocus />
+ </Button>
+
+</LinearLayout>
+
diff --git a/samples/Support4Demos/res/values/strings.xml b/samples/Support4Demos/res/values/strings.xml
index d0e096a..2649abc 100644
--- a/samples/Support4Demos/res/values/strings.xml
+++ b/samples/Support4Demos/res/values/strings.xml
@@ -110,6 +110,13 @@
<string name="start_service">Start Service</string>
<string name="stop_service">Stop Service</string>
+ <string name="simple_wakeful_controller">Content/Simple WakefulReceiver</string>
+
+ <string name="simple_wakeful_controller_msg">Demonstrates use of WakefulBroadcastReceiver
+ to keep the device awake while dispatching a broadcast to a service.</string>
+ <string name="schedule_wakeful_alarm">Wakeup in 30 seconds</string>
+ <string name="simple_wakeful_scheduled">Alarm scheduled</string>
+
<!-- Accessibility API -->
<string name="accessibility_manager_title">Accessibility/Accessibility Manager</string>
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/content/SimpleWakefulController.java b/samples/Support4Demos/src/com/example/android/supportv4/content/SimpleWakefulController.java
new file mode 100644
index 0000000..bf3b2c0
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/content/SimpleWakefulController.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.content;
+
+import com.example.android.supportv4.R;
+
+import android.app.Activity;
+import android.app.AlarmManager;
+import android.app.PendingIntent;
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.Button;
+import android.widget.Toast;
+
+import java.util.Calendar;
+
+public class SimpleWakefulController extends Activity {
+ Toast mToast;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ setContentView(R.layout.wakeful_alarm_controller);
+
+ // Watch for button clicks.
+ Button button = (Button)findViewById(R.id.schedule);
+ button.setOnClickListener(mScheduleListener);
+ }
+
+ private View.OnClickListener mScheduleListener = new View.OnClickListener() {
+ public void onClick(View v) {
+ // When the alarm goes off, we want to broadcast an Intent to our
+ // BroadcastReceiver. Here we make an Intent with an explicit class
+ // name to have our own receiver (which has been published in
+ // AndroidManifest.xml) instantiated and called, and then create an
+ // IntentSender to have the intent executed as a broadcast.
+ Intent intent = new Intent(SimpleWakefulController.this, SimpleWakefulReceiver.class);
+ PendingIntent sender = PendingIntent.getBroadcast(SimpleWakefulController.this,
+ 0, intent, 0);
+
+ // We want the alarm to go off 30 seconds from now.
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTimeInMillis(System.currentTimeMillis());
+ calendar.add(Calendar.SECOND, 30);
+
+ // Schedule the alarm!
+ AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
+ am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
+
+ // Tell the user about what we did.
+ if (mToast != null) {
+ mToast.cancel();
+ }
+ mToast = Toast.makeText(SimpleWakefulController.this, R.string.simple_wakeful_scheduled,
+ Toast.LENGTH_LONG);
+ mToast.show();
+ }
+ };
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/content/SimpleWakefulReceiver.java b/samples/Support4Demos/src/com/example/android/supportv4/content/SimpleWakefulReceiver.java
new file mode 100644
index 0000000..d70f432
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/content/SimpleWakefulReceiver.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.content;
+
+//BEGIN_INCLUDE(complete)
+import android.content.Context;
+import android.content.Intent;
+import android.os.SystemClock;
+import android.support.v4.content.WakefulBroadcastReceiver;
+import android.util.Log;
+
+public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ // This is the Intent to deliver to our service.
+ Intent service = new Intent(context, SimpleWakefulService.class);
+
+ // Start the service, keeping the device awake while it is launching.
+ Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
+ startWakefulService(context, service);
+ }
+}
+//END_INCLUDE(complete)
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/content/SimpleWakefulService.java b/samples/Support4Demos/src/com/example/android/supportv4/content/SimpleWakefulService.java
new file mode 100644
index 0000000..b05218f
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/content/SimpleWakefulService.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv4.content;
+
+//BEGIN_INCLUDE(complete)
+import android.app.IntentService;
+import android.content.Intent;
+import android.os.SystemClock;
+import android.util.Log;
+
+public class SimpleWakefulService extends IntentService {
+ public SimpleWakefulService() {
+ super("SimpleWakefulService");
+ }
+
+ @Override
+ protected void onHandleIntent(Intent intent) {
+ // At this point SimpleWakefulReceiver is still holding a wake lock
+ // for us. We can do whatever we need to here and then tell it that
+ // it can release the wakelock. This sample just does some slow work,
+ // but more complicated implementations could take their own wake
+ // lock here before releasing the receiver's.
+ //
+ // Note that when using this approach you should be aware that if your
+ // service gets killed and restarted while in the middle of such work
+ // (so the Intent gets re-delivered to perform the work again), it will
+ // at that point no longer be holding a wake lock since we are depending
+ // on SimpleWakefulReceiver to that for us. If this is a concern, you can
+ // acquire a separate wake lock here.
+ for (int i=0; i<5; i++) {
+ Log.i("SimpleWakefulReceiver", "Running service " + (i+1)
+ + "/5 @ " + SystemClock.elapsedRealtime());
+ try {
+ Thread.sleep(5000);
+ } catch (InterruptedException e) {
+ }
+ }
+ Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
+ SimpleWakefulReceiver.completeWakefulIntent(intent);
+ }
+}
+//END_INCLUDE(complete)