1 /* 2 * Copyright (C) 2016 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.android.cts.launcherapps.simpleapp; 18 19 import android.app.Notification; 20 import android.app.NotificationChannel; 21 import android.app.NotificationManager; 22 import android.app.PendingIntent; 23 import android.app.Service; 24 import android.content.Intent; 25 import android.os.Binder; 26 import android.os.IBinder; 27 import android.os.Parcel; 28 import android.os.Process; 29 import android.os.RemoteException; 30 import android.util.Log; 31 32 public class SimpleService extends Service { 33 private final static String TAG = SimpleService.class.getSimpleName(); 34 35 static final String ACTION_START_FOREGROUND = "com.android.test.action.START_FOREGROUND"; 36 static final String ACTION_STOP_FOREGROUND = "com.android.test.action.STOP_FOREGROUND"; 37 38 static final String CHANNEL_NAME = "SimpleService"; 39 40 final Binder mBinder = new Binder() { 41 @Override 42 protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) 43 throws RemoteException { 44 switch (code) { 45 case FIRST_CALL_TRANSACTION: 46 Process.killProcess(Process.myPid()); 47 return true; 48 } 49 return super.onTransact(code, data, reply, flags); 50 } 51 }; 52 53 @Override 54 public void onCreate() { 55 super.onCreate(); 56 Log.i(TAG, "onCreate called"); 57 } 58 59 @Override 60 public int onStartCommand(Intent intent, int flags, int startId) { 61 Log.i(TAG, "Starting: " + intent); 62 if (ACTION_START_FOREGROUND.equals(intent.getAction())) { 63 // Set the info for the views that show in the notification panel. 64 NotificationManager nm = getSystemService(NotificationManager.class); 65 if (nm.getNotificationChannel(CHANNEL_NAME) == null) { 66 nm.createNotificationChannel(new NotificationChannel(CHANNEL_NAME, CHANNEL_NAME, 67 NotificationManager.IMPORTANCE_DEFAULT)); 68 } 69 Notification notification = new Notification.Builder(this, CHANNEL_NAME) 70 .setSmallIcon(android.R.drawable.ic_popup_reminder) // the status icon 71 .setWhen(System.currentTimeMillis()) // the time stamp 72 .setContentTitle("This is a test notification") // the label 73 .setContentText("This is a test notification") // the contents of the entry 74 .build(); 75 startForeground(100, notification); 76 } else if (ACTION_STOP_FOREGROUND.equals(intent.getAction())) { 77 stopForeground(100); 78 } 79 return super.onStartCommand(intent, flags, startId); 80 } 81 82 @Override 83 public IBinder onBind(Intent intent) { 84 Log.i(TAG, "onBind called"); 85 return mBinder; 86 } 87 88 @Override 89 public void onDestroy() { 90 super.onDestroy(); 91 Log.i(TAG, "onDestroy called"); 92 } 93 } 94