从Android Wear通知回复中接收回复意图数据

时间:2016-11-15 18:08:13

标签: android wear-os voice-recognition

我正在尝试在我的Android Wear应用(https://developer.android.com/training/wearables/notifications/voice-input.html)上运行语音回复示例。我永远不会将结果记录到我的回复活动中。

我的MainActivity代码在这里:

package com.example.android.basicnotifications;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.RemoteInput;
import android.view.View;

/**
 * The entry point to the BasicNotification sample.
 */
public class MainActivity extends Activity {
    /**
     * A numeric value that identifies the notification that we'll be sending.
     * This value needs to be unique within this app, but it doesn't need to be
     * unique system-wide.
     */
    public static final int NOTIFICATION_ID = 1;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_layout);

    }

    /**
     * Send a sample notification using the NotificationCompat API.
     */

    public static final String EXTRA_VOICE_REPLY = "extra_voice_reply";
    public void sendNotification(View view) {

        String replyLabel = getResources().getString( R.string.reply_label );

        String []replyChoices = getResources().getStringArray(R.array.reply_choices);

        RemoteInput remoteInput = new RemoteInput.Builder( EXTRA_VOICE_REPLY )
            .setLabel( replyLabel )
                .setChoices( replyChoices )
            .build();


        Intent replyIntent = new Intent( this, ReplyActivity.class );
        PendingIntent replyPendingIntent = PendingIntent.getActivity(this, 0, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT );

        NotificationCompat.Action action = new NotificationCompat.Action.Builder( android.R.drawable.ic_dialog_email,
                getString( R.string.label ), replyPendingIntent )
                .addRemoteInput(remoteInput)
                .build();

        Notification notification = new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(android.R.drawable.ic_input_add)
//                .setLargeIcon(android.R.drawable.ic_dialog_dialer)
                .setContentTitle( "The title of the notification")
                .setContentText( "The text for the notification")
                .extend( new NotificationCompat.WearableExtender().addAction( action ))
                .build();

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
        int notificationId = 1000;
        notificationManager.notify( notificationId, notification );

    }

}

ReplyActivity在这里:

package com.example.android.basicnotifications;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.RemoteInput;
import android.util.Log;

/**
 * Created by cdawson on 11/14/16.
 */
public class ReplyActivity extends Activity{

    private CharSequence getMessageText( Intent intent ) {
        Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
        if (remoteInput != null) {
            return remoteInput.getCharSequence(MainActivity.EXTRA_VOICE_REPLY);
        }
        else {
            return "";
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_layout);

        Intent intent = getIntent();
        CharSequence cs = getMessageText( intent );

        Log.v( "ReplyActivity", cs.toString() );
//        CharSequence cs = getMessageText(savedInstanceState);


    }

}

具体来说,我不确定我在哪里调用函数getMessageText()。在ReplyActivity里面?当PendingIntent被解决时(这是正确的术语吗?)我想应用程序应该启动到该活动中,但是我不清楚当事情的磨损方面发起回复时会发生什么情况(这看起来很奇怪)当从手表发出回复时,在Android设备上启动一个全新的活动和UI。

0 个答案:

没有答案