basteez
basteez

Reputation: 477

Android Wear doesn't return voice data

I'm playing with wear SDK and I noticed a strange behaviour: if you try to catch voice input from wear by a BroadcastReceiver in your phone it doesn't work; it seems that wear needs an Activity to returns data to the phone.

Did you experienced that before?

Here's the code I'm using for my Activity:

package com.tizianobasile.weartest;
import android.app.Activity;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.RemoteInput;
import android.view.View;
import android.widget.Button;


public class MyActivity extends Activity {
    private Button notificationBtn;
    public static final String ACTION_DEMAND = "com.tizianobasile.weartest.MyActivity.ACTION_DEMAND";
    public static final String EXTRA_MESSAGE = "message";
    public static final String EXTRA_VOICE_REPLY = "reply";

    NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender().setHintShowBackgroundOnly(true);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        notificationBtn = (Button) findViewById(R.id.notificationBtn);
        final Activity a = this;
        notificationBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createNotification();
            }
        });
    }

    private void createNotification(){
        Intent demandIntent = new Intent(this, DemandReceiver.class)
                .putExtra(EXTRA_MESSAGE, "Reply Selected")
                .setAction(ACTION_DEMAND);
        PendingIntent demandPendingIntent = PendingIntent.getActivity(this, 0, demandIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        String replyLabel = getResources().getString(R.string.app_name);
        String[] replyActions = {"Yes", "No"};
        RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
                .setLabel(replyLabel)
                .setChoices(replyActions)
                .build();

        NotificationCompat.Action replyAction =
                new NotificationCompat.Action.Builder(R.drawable.ic_reply, "Reply", demandPendingIntent)
                        .addRemoteInput(remoteInput)
                        .build();

        NotificationCompat.WearableExtender wearableExtender =
                new NotificationCompat.WearableExtender()
                        .addAction(replyAction);

        long[] vibration = {0, 1000};

        Notification notification =
                new NotificationCompat.Builder(this)
                        .setContentTitle("Hello Wear")
                        .setContentText("You received a notification")
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setVibrate(vibration)
                        .extend(wearableExtender)
                        .build();

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

        notificationManager.notify(1, notification);
    }
}

And there's the BroadcastReceiver:

package com.tizianobasile.weartest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.RemoteInput;
import android.util.Log;
import android.widget.Toast;

public class DemandReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("WEAR", "Context: " + context.toString());
        Log.d("WEAR", "Intent: " + intent.toString());
        Log.d("WEAR", "Action" + intent.getAction());
        if(intent.getAction().equals(MyActivity.ACTION_DEMAND)){
            String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
            Log.d("WEAR", "Message from intent: " + message);
            Toast.makeText(context, "Message from intent: " + message, Toast.LENGTH_LONG).show();
            Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
            CharSequence reply = remoteInput.getCharSequence(MyActivity.EXTRA_VOICE_REPLY);
            Log.d("WEAR", "User replies: " + reply);
            Toast.makeText(context, "User replies: " + reply, Toast.LENGTH_LONG).show();
        }
    }
}

Upvotes: 0

Views: 528

Answers (1)

Maciej Ciemięga
Maciej Ciemięga

Reputation: 10715

While creating your demandPendingIntent you basically tell that this will be an Activity:

    Intent demandIntent = new Intent(this, DemandReceiver.class)
            .putExtra(EXTRA_MESSAGE, "Reply Selected")
            .setAction(ACTION_DEMAND);
    PendingIntent demandPendingIntent = PendingIntent.getActivity(this, 0, demandIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Instead of using:
PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags)
you need to use:
PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags)

This will let system know that you want to handle this PendingIntent by Broadcastreceiver.

Upvotes: 2

Related Questions