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"> + @@ -242,6 +243,17 @@ + + + + + + + + + + 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 @@ + + + + + + + + + + + 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 @@ Start Service Stop Service + Content/Simple WakefulReceiver + + Demonstrates use of WakefulBroadcastReceiver + to keep the device awake while dispatching a broadcast to a service. + Wakeup in 30 seconds + Alarm scheduled + Accessibility/Accessibility Manager 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)