UpdateService.java revision 20f6c51ab018c45347bc99ccccb17f45cbfb9fc0
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
17package com.android.example.notificationshowcase;
18
19import android.app.IntentService;
20import android.app.NotificationManager;
21import android.app.PendingIntent;
22import android.content.Context;
23import android.content.Intent;
24import android.content.SharedPreferences;
25import android.support.v7.preference.PreferenceManager;
26import android.util.Log;
27
28public class UpdateService extends IntentService {
29
30    private static final String TAG = "UpdateService";
31
32    private static final String ACTION_UPDATE = "update";
33
34    public UpdateService() {
35        super(TAG);
36    }
37
38    public UpdateService(String name) {
39        super(name);
40    }
41
42    @Override
43    protected void onHandleIntent(Intent intent) {
44        Log.v(TAG, "clicked a thing! intent=" + intent.toString());
45        if (intent.hasExtra("id") && intent.hasExtra("when")) {
46            final int id = intent.getIntExtra("id", 0);
47                NotificationManager noMa =
48                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
49                final int update = intent.getIntExtra("update", 0);
50                final long when = intent.getLongExtra("when", 0L);
51                Log.v(TAG, "id: " + id + " when: " + when + " update: " + update);
52                noMa.notify(NotificationService.NOTIFICATION_ID + id,
53                        NotificationService.makeSmsNotification(this, update, id, when));
54        } else {
55            Log.v(TAG, "id extra was " + (intent.hasExtra("id") ? "present" : "missing"));
56            Log.v(TAG, "when extra was " + (intent.hasExtra("when") ? "present"  : "missing"));
57        }
58    }
59
60    public static PendingIntent getPendingIntent(Context context, int update, int id, long when) {
61        Intent updateIntent = new Intent(context, UpdateService.class);
62        updateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
63        updateIntent.setAction(ACTION_UPDATE);
64        updateIntent.putExtra("id", id);
65        updateIntent.putExtra("when", when);
66        updateIntent.putExtra("update", update);
67        PendingIntent pi = PendingIntent.getService(
68                context, 58, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
69        return pi;
70    }
71}
72