Skip to content

Commit 041bfcd

Browse files
author
Dianne Hackborn
committed
Fix issue #8520788: Add WakefulBroadcastReceiver
Add sample code. Change-Id: I46f4f25ceb4e12f975e3cebd83f62de2555a8996
1 parent fb6dc4f commit 041bfcd

File tree

7 files changed

+222
-1
lines changed

7 files changed

+222
-1
lines changed

samples/ApiDemos/src/com/example/android/apis/media/VideoViewDemo.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,5 @@ public void onCreate(Bundle icicle) {
4646
"/" + R.raw.videoviewdemo));
4747
mVideoView.setMediaController(new MediaController(this));
4848
mVideoView.requestFocus();
49-
5049
}
5150
}

samples/Support4Demos/AndroidManifest.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package="com.example.android.supportv4">
2424

2525
<uses-permission android:name="android.permission.READ_CONTACTS" />
26+
<uses-permission android:name="android.permission.WAKE_LOCK" />
2627

2728
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="13" />
2829

@@ -242,6 +243,17 @@
242243
<service android:name=".content.LocalServiceBroadcaster$LocalService"
243244
android:stopWithTask="true" />
244245

246+
<activity android:name=".content.SimpleWakefulController"
247+
android:label="@string/simple_wakeful_controller">
248+
<intent-filter>
249+
<action android:name="android.intent.action.MAIN" />
250+
<category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
251+
</intent-filter>
252+
</activity>
253+
254+
<receiver android:name=".content.SimpleWakefulReceiver" />
255+
<service android:name=".content.SimpleWakefulService" />
256+
245257
<activity android:name=".accessibility.AccessibilityManagerSupportActivity"
246258
android:label="@string/accessibility_manager_title">
247259
<intent-filter>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright (C) 2013 The Android Open Source Project
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
18+
android:orientation="vertical" android:padding="4dip"
19+
android:gravity="center_horizontal"
20+
android:layout_width="match_parent" android:layout_height="match_parent">
21+
22+
<TextView
23+
android:layout_width="match_parent" android:layout_height="wrap_content"
24+
android:layout_weight="0" android:paddingBottom="4dip"
25+
android:textAppearance="?android:attr/textAppearanceMedium"
26+
android:text="@string/simple_wakeful_controller_msg"/>
27+
28+
<Button android:id="@+id/schedule"
29+
android:layout_width="wrap_content" android:layout_height="wrap_content"
30+
android:text="@string/schedule_wakeful_alarm">
31+
<requestFocus />
32+
</Button>
33+
34+
</LinearLayout>
35+

samples/Support4Demos/res/values/strings.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@
110110
<string name="start_service">Start Service</string>
111111
<string name="stop_service">Stop Service</string>
112112

113+
<string name="simple_wakeful_controller">Content/Simple WakefulReceiver</string>
114+
115+
<string name="simple_wakeful_controller_msg">Demonstrates use of WakefulBroadcastReceiver
116+
to keep the device awake while dispatching a broadcast to a service.</string>
117+
<string name="schedule_wakeful_alarm">Wakeup in 30 seconds</string>
118+
<string name="simple_wakeful_scheduled">Alarm scheduled</string>
119+
113120
<!-- Accessibility API -->
114121

115122
<string name="accessibility_manager_title">Accessibility/Accessibility Manager</string>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (C) 2013 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.supportv4.content;
18+
19+
import com.example.android.supportv4.R;
20+
21+
import android.app.Activity;
22+
import android.app.AlarmManager;
23+
import android.app.PendingIntent;
24+
import android.content.Intent;
25+
import android.os.Bundle;
26+
import android.view.View;
27+
import android.widget.Button;
28+
import android.widget.Toast;
29+
30+
import java.util.Calendar;
31+
32+
public class SimpleWakefulController extends Activity {
33+
Toast mToast;
34+
35+
@Override
36+
protected void onCreate(Bundle savedInstanceState) {
37+
super.onCreate(savedInstanceState);
38+
39+
setContentView(R.layout.wakeful_alarm_controller);
40+
41+
// Watch for button clicks.
42+
Button button = (Button)findViewById(R.id.schedule);
43+
button.setOnClickListener(mScheduleListener);
44+
}
45+
46+
private View.OnClickListener mScheduleListener = new View.OnClickListener() {
47+
public void onClick(View v) {
48+
// When the alarm goes off, we want to broadcast an Intent to our
49+
// BroadcastReceiver. Here we make an Intent with an explicit class
50+
// name to have our own receiver (which has been published in
51+
// AndroidManifest.xml) instantiated and called, and then create an
52+
// IntentSender to have the intent executed as a broadcast.
53+
Intent intent = new Intent(SimpleWakefulController.this, SimpleWakefulReceiver.class);
54+
PendingIntent sender = PendingIntent.getBroadcast(SimpleWakefulController.this,
55+
0, intent, 0);
56+
57+
// We want the alarm to go off 30 seconds from now.
58+
Calendar calendar = Calendar.getInstance();
59+
calendar.setTimeInMillis(System.currentTimeMillis());
60+
calendar.add(Calendar.SECOND, 30);
61+
62+
// Schedule the alarm!
63+
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
64+
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
65+
66+
// Tell the user about what we did.
67+
if (mToast != null) {
68+
mToast.cancel();
69+
}
70+
mToast = Toast.makeText(SimpleWakefulController.this, R.string.simple_wakeful_scheduled,
71+
Toast.LENGTH_LONG);
72+
mToast.show();
73+
}
74+
};
75+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) 2013 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.supportv4.content;
18+
19+
//BEGIN_INCLUDE(complete)
20+
import android.content.Context;
21+
import android.content.Intent;
22+
import android.os.SystemClock;
23+
import android.support.v4.content.WakefulBroadcastReceiver;
24+
import android.util.Log;
25+
26+
public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
27+
@Override
28+
public void onReceive(Context context, Intent intent) {
29+
// This is the Intent to deliver to our service.
30+
Intent service = new Intent(context, SimpleWakefulService.class);
31+
32+
// Start the service, keeping the device awake while it is launching.
33+
Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
34+
startWakefulService(context, service);
35+
}
36+
}
37+
//END_INCLUDE(complete)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2013 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.supportv4.content;
18+
19+
//BEGIN_INCLUDE(complete)
20+
import android.app.IntentService;
21+
import android.content.Intent;
22+
import android.os.SystemClock;
23+
import android.util.Log;
24+
25+
public class SimpleWakefulService extends IntentService {
26+
public SimpleWakefulService() {
27+
super("SimpleWakefulService");
28+
}
29+
30+
@Override
31+
protected void onHandleIntent(Intent intent) {
32+
// At this point SimpleWakefulReceiver is still holding a wake lock
33+
// for us. We can do whatever we need to here and then tell it that
34+
// it can release the wakelock. This sample just does some slow work,
35+
// but more complicated implementations could take their own wake
36+
// lock here before releasing the receiver's.
37+
//
38+
// Note that when using this approach you should be aware that if your
39+
// service gets killed and restarted while in the middle of such work
40+
// (so the Intent gets re-delivered to perform the work again), it will
41+
// at that point no longer be holding a wake lock since we are depending
42+
// on SimpleWakefulReceiver to that for us. If this is a concern, you can
43+
// acquire a separate wake lock here.
44+
for (int i=0; i<5; i++) {
45+
Log.i("SimpleWakefulReceiver", "Running service " + (i+1)
46+
+ "/5 @ " + SystemClock.elapsedRealtime());
47+
try {
48+
Thread.sleep(5000);
49+
} catch (InterruptedException e) {
50+
}
51+
}
52+
Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
53+
SimpleWakefulReceiver.completeWakefulIntent(intent);
54+
}
55+
}
56+
//END_INCLUDE(complete)

0 commit comments

Comments
 (0)