diff options
| author | Dianne Hackborn <hackbod@google.com> | 2013-04-17 10:17:03 -0700 |
|---|---|---|
| committer | Dianne Hackborn <hackbod@google.com> | 2013-04-17 10:33:16 -0700 |
| commit | 041bfcd9e4e1c81c474f846637a73ec119749e1a (patch) | |
| tree | 3b700808d50f7ba08727dd7f9a63b86947796a36 /samples | |
| parent | fb6dc4fedf1034e61a792d24094b7f74854f70d4 (diff) | |
| download | android_development-041bfcd9e4e1c81c474f846637a73ec119749e1a.tar.gz android_development-041bfcd9e4e1c81c474f846637a73ec119749e1a.tar.bz2 android_development-041bfcd9e4e1c81c474f846637a73ec119749e1a.zip | |
Fix issue #8520788: Add WakefulBroadcastReceiver
Add sample code.
Change-Id: I46f4f25ceb4e12f975e3cebd83f62de2555a8996
Diffstat (limited to 'samples')
7 files changed, 222 insertions, 1 deletions
diff --git a/samples/ApiDemos/src/com/example/android/apis/media/VideoViewDemo.java b/samples/ApiDemos/src/com/example/android/apis/media/VideoViewDemo.java index 5143b4fe3..dacff56f2 100644 --- a/samples/ApiDemos/src/com/example/android/apis/media/VideoViewDemo.java +++ b/samples/ApiDemos/src/com/example/android/apis/media/VideoViewDemo.java @@ -46,6 +46,5 @@ public class VideoViewDemo extends Activity { "/" + R.raw.videoviewdemo)); mVideoView.setMediaController(new MediaController(this)); mVideoView.requestFocus(); - } } diff --git a/samples/Support4Demos/AndroidManifest.xml b/samples/Support4Demos/AndroidManifest.xml index 8d20c9efd..ce33ec1c6 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 000000000..725331d78 --- /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 d0e096a4c..2649abc8a 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 000000000..bf3b2c05b --- /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 000000000..d70f432f8 --- /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 000000000..b05218f17 --- /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) |
