Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2017 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.apis.app;
     18 
     19 // Need the following import to get access to the app resources, since this
     20 // class is in a sub-package.
     21 import com.example.android.apis.R;
     22 
     23 import android.app.Activity;
     24 import android.app.Notification;
     25 import android.app.NotificationManager;
     26 import android.app.PendingIntent;
     27 import android.app.Service;
     28 import android.content.Context;
     29 import android.content.Intent;
     30 import android.os.Bundle;
     31 import android.os.IBinder;
     32 import android.view.View;
     33 import android.view.View.OnClickListener;
     34 import android.widget.Button;
     35 
     36 /**
     37  * Example service that gets launched from a notification and runs in the background.
     38  */
     39 public class NotificationBackgroundService extends Service {
     40     @Override
     41     public int onStartCommand(Intent intent, int flags, int startId) {
     42         ((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE))
     43                 .cancel(R.layout.notification_background_service);
     44         stopSelf(startId);
     45         return START_NOT_STICKY;
     46     }
     47 
     48     @Override
     49     public IBinder onBind(Intent intent) {
     50         return null;
     51     }
     52 
     53     /**
     54      * Demo UI that allows the user to post the notification.
     55      */
     56     public static class Controller extends Activity {
     57         private NotificationManager mNM;
     58 
     59         @Override
     60         protected void onCreate(Bundle savedInstanceState) {
     61             super.onCreate(savedInstanceState);
     62 
     63             mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
     64 
     65             setContentView(R.layout.notification_background_service);
     66 
     67             Button button = (Button) findViewById(R.id.notify);
     68             button.setOnClickListener(mNotify);
     69         }
     70 
     71         private void showNotification(CharSequence text) {
     72             // The PendingIntent to launch our activity if the user selects this notification
     73             PendingIntent contentIntent = PendingIntent.getService(this, 0,
     74                     new Intent(this, NotificationBackgroundService.class), 0);
     75 
     76             // Set the info for the views that show in the notification panel.
     77             Notification notification = new Notification.Builder(this)
     78                     .setSmallIcon(R.drawable.stat_sample)  // the status icon
     79                     .setTicker(text)  // the status text
     80                     .setWhen(System.currentTimeMillis())  // the time stamp
     81                     .setContentTitle(getText(R.string.notification_background_label))  // the label of the entry
     82                     .setContentText(text)  // the contents of the entry
     83                     .setContentIntent(contentIntent)  // The intent to send when the entry is clicked
     84                     .build();
     85 
     86             // Send the notification.
     87             // We use a layout id because it is a unique number.  We use it later to cancel.
     88             mNM.notify(R.layout.notification_background_service, notification);
     89         }
     90 
     91         private OnClickListener mNotify = new OnClickListener() {
     92             public void onClick(View v) {
     93                 showNotification("Selecting this will cause a background service to run.");
     94             }
     95         };
     96     }
     97 }
     98 
     99