Skip to content

Most visited

Recently visited

navigation
Quiz / Wearable / src / com.example.android.wearable.quiz /

DeleteQuestionService.java

1
/*
2
 * Copyright (C) 2014 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.example.android.wearable.quiz;
18
 
19
import static com.example.android.wearable.quiz.Constants.CONNECT_TIMEOUT_MS;
20
import static com.example.android.wearable.quiz.Constants.QUESTION_WAS_DELETED;
21
 
22
import android.app.IntentService;
23
import android.content.Intent;
24
import android.net.Uri;
25
import android.os.Bundle;
26
import android.util.Log;
27
 
28
import com.google.android.gms.common.ConnectionResult;
29
import com.google.android.gms.common.api.GoogleApiClient;
30
import com.google.android.gms.wearable.DataApi;
31
import com.google.android.gms.wearable.DataMap;
32
import com.google.android.gms.wearable.DataMapItem;
33
import com.google.android.gms.wearable.PutDataMapRequest;
34
import com.google.android.gms.wearable.PutDataRequest;
35
import com.google.android.gms.wearable.Wearable;
36
 
37
import java.util.concurrent.TimeUnit;
38
 
39
/**
40
 * Used to update quiz status on the phone when user dismisses a question on the watch.
41
 */
42
public class DeleteQuestionService extends IntentService
43
        implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
44
 
45
    private static final String TAG = "DeleteQuestionReceiver";
46
 
47
    private GoogleApiClient mGoogleApiClient;
48
 
49
    public DeleteQuestionService() {
50
        super(DeleteQuestionService.class.getSimpleName());
51
    }
52
 
53
    @Override
54
    public void onCreate() {
55
        super.onCreate();
56
        mGoogleApiClient = new GoogleApiClient.Builder(this)
57
                .addApi(Wearable.API)
58
                .addConnectionCallbacks(this)
59
                .addOnConnectionFailedListener(this)
60
                .build();
61
    }
62
 
63
    @Override
64
    public void onHandleIntent(Intent intent) {
65
        mGoogleApiClient.blockingConnect(CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS);
66
        Uri dataItemUri = intent.getData();
67
        if (!mGoogleApiClient.isConnected()) {
68
            Log.e(TAG, "Failed to update data item " + dataItemUri
69
                    + " because client is disconnected from Google Play Services");
70
            return;
71
        }
72
        DataApi.DataItemResult dataItemResult = Wearable.DataApi.getDataItem(
73
                mGoogleApiClient, dataItemUri).await();
74
        PutDataMapRequest putDataMapRequest = PutDataMapRequest
75
                .createFromDataMapItem(DataMapItem.fromDataItem(dataItemResult.getDataItem()));
76
        DataMap dataMap = putDataMapRequest.getDataMap();
77
        dataMap.putBoolean(QUESTION_WAS_DELETED, true);
78
        PutDataRequest request = putDataMapRequest.asPutDataRequest();
79
        request.setUrgent();
80
        Wearable.DataApi.putDataItem(mGoogleApiClient, request).await();
81
        mGoogleApiClient.disconnect();
82
    }
83
 
84
    @Override
85
    public void onConnected(Bundle bundle) {
86
    }
87
 
88
    @Override
89
    public void onConnectionSuspended(int i) {
90
    }
91
 
92
    @Override
93
    public void onConnectionFailed(ConnectionResult connectionResult) {
94
    }
95
}
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.