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.android.example.notificationshowcase; 18 19 import android.app.IntentService; 20 import android.app.NotificationManager; 21 import android.app.PendingIntent; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.os.Handler; 25 import android.util.Log; 26 import android.widget.Toast; 27 28 public class PhoneService extends IntentService { 29 30 private static final String TAG = "PhoneService"; 31 32 public static final String ACTION_ANSWER = "answer"; 33 public static final String ACTION_IGNORE = "ignore"; 34 35 public static final String EXTRA_ID = "id"; 36 37 private Handler handler; 38 39 public PhoneService() { 40 super(TAG); 41 } 42 public PhoneService(String name) { 43 super(name); 44 } 45 46 @Override 47 public int onStartCommand(Intent intent, int flags, int startId) { 48 handler = new Handler(); 49 return super.onStartCommand(intent, flags, startId); 50 } 51 52 @Override 53 protected void onHandleIntent(Intent intent) { 54 Log.v(TAG, "clicked a thing! intent=" + intent.toString()); 55 int res = ACTION_ANSWER.equals(intent.getAction()) ? R.string.answered : R.string.ignored; 56 final String text = getString(res); 57 final int id = intent.getIntExtra(EXTRA_ID, -1); 58 handler.post(new Runnable() { 59 @Override 60 public void run() { 61 Toast.makeText(PhoneService.this, text, Toast.LENGTH_LONG).show(); 62 if (id >= 0) { 63 NotificationManager noMa = 64 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 65 noMa.cancel(NotificationService.NOTIFICATION_ID + id); 66 } 67 Log.v(TAG, "phone toast " + text); 68 } 69 }); 70 } 71 72 public static PendingIntent getPendingIntent(Context context, int id, String action) { 73 Intent phoneIntent = new Intent(context, PhoneService.class); 74 phoneIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 75 phoneIntent.setAction(action); 76 phoneIntent.putExtra(EXTRA_ID, id); 77 PendingIntent pi = PendingIntent.getService( 78 context, 58, phoneIntent, PendingIntent.FLAG_UPDATE_CURRENT); 79 return pi; 80 } 81 } 82