1 package com.example.android.wearable.watchface.provider;
2 
3 import android.app.IntentService;
4 import android.content.ComponentName;
5 import android.content.Intent;
6 import android.support.wearable.complications.ProviderUpdateRequester;
7 import android.util.Log;
8 
9 /**
10  * Simple {@link IntentService} subclass for asynchronously requesting an update for the random
11  * number complication (triggered via TapAction on complication).
12  */
13 public class UpdateComplicationDataService extends IntentService {
14 
15     private static final String TAG = "UpdateCompService";
16 
17     public static final String ACTION_UPDATE_COMPLICATION =
18             "com.example.android.wearable.watchface.provider.action.UPDATE_COMPLICATION";
19 
20     public static final String EXTRA_COMPLICATION_ID =
21             "com.example.android.wearable.watchface.provider.action.COMPLICATION_ID";
22 
UpdateComplicationDataService()23     public UpdateComplicationDataService() {
24         super("UpdateComplicationDataService");
25     }
26 
27     @Override
onHandleIntent(Intent intent)28     protected void onHandleIntent(Intent intent) {
29 
30         if (intent != null) {
31 
32             final String action = intent.getAction();
33 
34             if (ACTION_UPDATE_COMPLICATION.equals(action)) {
35 
36                 int complicationId = intent.getIntExtra(EXTRA_COMPLICATION_ID, -1);
37                 handleActionUpdateComplicationData(complicationId);
38             }
39         }
40     }
41 
42     /**
43      * Handle action UpdateComplicationData in the provided background thread with the provided
44      * parameters.
45      */
handleActionUpdateComplicationData(int complicationId)46     private void handleActionUpdateComplicationData(int complicationId) {
47 
48         Log.d(TAG, "Complication id to update via service: " + complicationId);
49 
50         ComponentName componentName =
51                 new ComponentName(getApplicationContext(), RandomNumberProviderService.class);
52 
53         ProviderUpdateRequester providerUpdateRequester =
54                 new ProviderUpdateRequester(getApplicationContext(), componentName);
55 
56         if (complicationId > 0) {
57             // This method only updates the specific complication tapped on the watch, if you
58             // wanted to update all active complications associated with your data, you would
59             // call providerUpdateRequester.requestUpdateAll().
60             providerUpdateRequester.requestUpdate(complicationId);
61         }
62     }
63 }