Skip to content

Most visited

Recently visited

navigation
Notifications / Application / src / com.example.android.support.wearable.notifications /

NotificationIntentReceiver.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.support.wearable.notifications;
18
 
19
import android.content.BroadcastReceiver;
20
import android.content.Context;
21
import android.content.Intent;
22
import android.os.Bundle;
23
import android.support.v4.app.RemoteInput;
24
import android.widget.Toast;
25
 
26
/**
27
 * Broadcast receiver to post toast messages in response to notification intents firing.
28
 */
29
public class NotificationIntentReceiver extends BroadcastReceiver {
30
    public static final String ACTION_EXAMPLE =
31
            "com.example.android.support.wearable.notifications.ACTION_EXAMPLE";
32
    public static final String ACTION_ENABLE_MESSAGES =
33
            "com.example.android.support.wearable.notifications.ACTION_ENABLE_MESSAGES";
34
    public static final String ACTION_DISABLE_MESSAGES =
35
            "com.example.android.support.wearable.notifications.ACTION_DISABLE_MESSAGES";
36
 
37
    private boolean mEnableMessages = true;
38
 
39
    @Override
40
    public void onReceive(Context context, Intent intent) {
41
        if (intent.getAction().equals(ACTION_EXAMPLE)) {
42
            if (mEnableMessages) {
43
                String message = intent.getStringExtra(NotificationUtil.EXTRA_MESSAGE);
44
                Bundle remoteInputResults = RemoteInput.getResultsFromIntent(intent);
45
                CharSequence replyMessage = null;
46
                if (remoteInputResults != null) {
47
                    replyMessage = remoteInputResults.getCharSequence(NotificationUtil.EXTRA_REPLY);
48
                }
49
                if (replyMessage != null) {
50
                    message = message + ": \"" + replyMessage + "\"";
51
                }
52
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
53
            }
54
        } else if (intent.getAction().equals(ACTION_ENABLE_MESSAGES)) {
55
            mEnableMessages = true;
56
        } else if (intent.getAction().equals(ACTION_DISABLE_MESSAGES)) {
57
            mEnableMessages = false;
58
        }
59
    }
60
}
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

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.