Skip to content

Most visited

Recently visited

navigation
WatchFace / Wearable / src / com.example.android.wearable.watchface / provider /

UpdateComplicationDataService.java

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
 
23
    public UpdateComplicationDataService() {
24
        super("UpdateComplicationDataService");
25
    }
26
 
27
    @Override
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
     */
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
}
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.