diff options
| author | Dianne Hackborn <hackbod@google.com> | 2017-01-20 17:20:53 -0800 |
|---|---|---|
| committer | Dianne Hackborn <hackbod@google.com> | 2017-01-23 14:20:03 -0800 |
| commit | 8a416b3033d08906483a89d8bf2beeb1b85001a0 (patch) | |
| tree | d17f867ca7817e614ce4365a350c2ad14c2c6d3d /samples/ApiDemos | |
| parent | 02a314b14c87f1b85843522719a1ae007ac3a872 (diff) | |
| download | android_development-8a416b3033d08906483a89d8bf2beeb1b85001a0.tar.gz android_development-8a416b3033d08906483a89d8bf2beeb1b85001a0.tar.bz2 android_development-8a416b3033d08906483a89d8bf2beeb1b85001a0.zip | |
API demo for notification that starts a background service.
Test: not relevant
Change-Id: I9ad6640dccded3be0baf879a6694f9535144d80c
Diffstat (limited to 'samples/ApiDemos')
4 files changed, 149 insertions, 0 deletions
diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml index b4a619d7b..0d626d4f6 100644 --- a/samples/ApiDemos/AndroidManifest.xml +++ b/samples/ApiDemos/AndroidManifest.xml @@ -786,6 +786,15 @@ </intent-filter> </activity> + <service android:name=".app.NotificationBackgroundService" /> + <activity android:name=".app.NotificationBackgroundService$Controller" + android:label="App/Notification/Background Service"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.SAMPLE_CODE" /> + </intent-filter> + </activity> + <!-- Dialog samples --> <activity android:name=".app.AlertDialogSamples" android:label="@string/activity_alert_dialog"> <intent-filter> diff --git a/samples/ApiDemos/res/layout/notification_background_service.xml b/samples/ApiDemos/res/layout/notification_background_service.xml new file mode 100644 index 000000000..93a6a14db --- /dev/null +++ b/samples/ApiDemos/res/layout/notification_background_service.xml @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2017 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. +--> + +<!-- Demonstrates starting and stopping a local service. + See corresponding Java code com.android.sdk.app.LocalSerice.java. --> + +<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:text="@string/notification_background_service"/> + + <Button android:id="@+id/notify" + android:layout_width="wrap_content" android:layout_height="wrap_content" + android:text="@string/notify_bg"> + <requestFocus /> + </Button> + +</LinearLayout> diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml index 437cca264..37e55f7b5 100644 --- a/samples/ApiDemos/res/values/strings.xml +++ b/samples/ApiDemos/res/values/strings.xml @@ -375,6 +375,12 @@ <string name="notifying_service_controller">This service will update a status bar notification every 5 seconds for a minute</string> + <string name="notification_background_service">This shows how you can use a notification to + send a broadcast and allow you to start a service even if your app is in the + background.</string> + <string name="notify_bg">Post notification</string> + <string name="notification_background_label">Notification to background</string> + <string name="activity_custom_title">App/Activity/Custom Title</string> <string name="custom_title_left">Left is best</string> <string name="custom_title_right">Right is always right</string> diff --git a/samples/ApiDemos/src/com/example/android/apis/app/NotificationBackgroundService.java b/samples/ApiDemos/src/com/example/android/apis/app/NotificationBackgroundService.java new file mode 100644 index 000000000..bd5f0601f --- /dev/null +++ b/samples/ApiDemos/src/com/example/android/apis/app/NotificationBackgroundService.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2017 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.apis.app; + +// 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; + +import android.app.Activity; +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.IBinder; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; + +/** + * Example service that gets launched from a notification and runs in the background. + */ +public class NotificationBackgroundService extends Service { + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + ((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)) + .cancel(R.layout.notification_background_service); + stopSelf(startId); + return START_NOT_STICKY; + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + /** + * Demo UI that allows the user to post the notification. + */ + public static class Controller extends Activity { + private NotificationManager mNM; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + + setContentView(R.layout.notification_background_service); + + Button button = (Button) findViewById(R.id.notify); + button.setOnClickListener(mNotify); + } + + private void showNotification(CharSequence text) { + // The PendingIntent to launch our activity if the user selects this notification + PendingIntent contentIntent = PendingIntent.getService(this, 0, + new Intent(this, NotificationBackgroundService.class), 0); + + // Set the info for the views that show in the notification panel. + Notification notification = new Notification.Builder(this) + .setSmallIcon(R.drawable.stat_sample) // the status icon + .setTicker(text) // the status text + .setWhen(System.currentTimeMillis()) // the time stamp + .setContentTitle(getText(R.string.notification_background_label)) // the label of the entry + .setContentText(text) // the contents of the entry + .setContentIntent(contentIntent) // The intent to send when the entry is clicked + .build(); + + // Send the notification. + // We use a layout id because it is a unique number. We use it later to cancel. + mNM.notify(R.layout.notification_background_service, notification); + } + + private OnClickListener mNotify = new OnClickListener() { + public void onClick(View v) { + showNotification("Selecting this will cause a background service to run."); + } + }; + } +} + |
