19bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren/* 29bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren * Copyright (C) 2013 The Android Open Source Project 39bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren * 49bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren * Licensed under the Apache License, Version 2.0 (the "License"); 59bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren * you may not use this file except in compliance with the License. 69bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren * You may obtain a copy of the License at 79bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren * 89bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren * http://www.apache.org/licenses/LICENSE-2.0 99bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren * 109bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren * Unless required by applicable law or agreed to in writing, software 119bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren * distributed under the License is distributed on an "AS IS" BASIS, 129bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren * See the License for the specific language governing permissions and 149bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren * limitations under the License. 159bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren */ 169bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren 179bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wrenpackage com.android.example.notificationshowcase; 189bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren 199bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wrenimport android.app.IntentService; 209bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wrenimport android.app.NotificationManager; 219bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wrenimport android.app.PendingIntent; 229bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wrenimport android.content.Context; 239bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wrenimport android.content.Intent; 249bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wrenimport android.os.Handler; 259bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wrenimport android.util.Log; 269bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wrenimport android.widget.Toast; 279bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren 289bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wrenpublic class PhoneService extends IntentService { 299bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren 309bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren private static final String TAG = "PhoneService"; 319bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren 329bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren public static final String ACTION_ANSWER = "answer"; 339bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren public static final String ACTION_IGNORE = "ignore"; 349bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren 359bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren public static final String EXTRA_ID = "id"; 369bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren 379bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren private Handler handler; 389bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren 399bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren public PhoneService() { 409bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren super(TAG); 419bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren } 429bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren public PhoneService(String name) { 439bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren super(name); 449bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren } 459bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren 469bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren @Override 479bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren public int onStartCommand(Intent intent, int flags, int startId) { 489bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren handler = new Handler(); 499bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren return super.onStartCommand(intent, flags, startId); 509bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren } 519bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren 529bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren @Override 539bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren protected void onHandleIntent(Intent intent) { 549bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren Log.v(TAG, "clicked a thing! intent=" + intent.toString()); 559bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren int res = ACTION_ANSWER.equals(intent.getAction()) ? R.string.answered : R.string.ignored; 569bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren final String text = getString(res); 579bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren final int id = intent.getIntExtra(EXTRA_ID, -1); 589bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren handler.post(new Runnable() { 599bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren @Override 609bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren public void run() { 619bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren Toast.makeText(PhoneService.this, text, Toast.LENGTH_LONG).show(); 629bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren if (id >= 0) { 639bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren NotificationManager noMa = 649bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 659bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren noMa.cancel(NotificationService.NOTIFICATION_ID + id); 669bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren } 679bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren Log.v(TAG, "phone toast " + text); 689bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren } 699bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren }); 709bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren } 719bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren 729bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren public static PendingIntent getPendingIntent(Context context, int id, String action) { 739bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren Intent phoneIntent = new Intent(context, PhoneService.class); 749bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren phoneIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 759bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren phoneIntent.setAction(action); 769bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren phoneIntent.putExtra(EXTRA_ID, id); 779bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren PendingIntent pi = PendingIntent.getService( 789bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren context, 58, phoneIntent, PendingIntent.FLAG_UPDATE_CURRENT); 799bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren return pi; 809bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren } 819bf14882a56bfd5e0bd415993da7d37d8b5c7eb6Chris Wren} 82