Voice input with Android Wear Preview

妖精的绣舞 提交于 2019-12-08 08:32:22

问题


I'm trying the new library to develop Android apps with wearable device (the Android Wear). I understood how the library works, now I'm trying to develop a notification for the code I found in this tutorial, but when I simulate a notification with a simply button it doesn't do nothing and I don't know why... I post here my code I hope you can help me to understand why it's not working:

public class MainActivity extends Activity {
    private NotificationManager mNotificationManager;
    private int notificationID = 100;
    private int numMessages = 0;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button voiceBtn = (Button) findViewById(R.id.voice);
        voiceBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                voiceNotification();

            }
        });
    }

protected void voiceNotification() {
    // Creo l'istanza di RemoteInput
    final String EXTRA_VOICE_REPLY = "extra_voice_reply";
    String replyLabel = getResources().getString(R.string.reply_label);
    String[] replyChoices = getResources().getStringArray(
            R.array.reply_choices);

    // Creo l'intent
    Intent replyIntent = new Intent(this, ReplyActivity.class);
    PendingIntent replyPendingIntent = PendingIntent.getActivity(this, 0,
            replyIntent, 0);

    // Creo la notifica
    NotificationCompat.Builder replyNotificationBuilder = new NotificationCompat.Builder(
            this);
    replyNotificationBuilder
            .setSmallIcon(android.R.drawable.ic_btn_speak_now);
    replyNotificationBuilder.setContentTitle("Messaggio");
    replyNotificationBuilder.setContentText("Parla ora");
    replyNotificationBuilder.setContentIntent(replyPendingIntent);

    replyNotificationBuilder.setNumber(++numMessages);

    mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(notificationID,replyNotificationBuilder.build());

     //Creo il remote input
     RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
     .setLabel(replyLabel)
     .build();

     // Creo l'azione da associare alla notifica
     Action replyAction = new
     Action.Builder(android.R.drawable.ic_btn_speak_now, "Rispondi",
     replyPendingIntent)
     .addRemoteInput(remoteInput)
     .build();

     // Creo la notifica wearable con il remote input
     Notification replyNotification = new
     WearableNotifications.Builder(replyNotificationBuilder)
     .addAction(replyAction)
     .build();

}

I followed the tutorial directly from Android developers page, but it's not working. What's wrong in the code I posted?


回答1:


I solved my issued by using the following code:

protected void voiceNotification() {

        // Creo l'intent per l'azione di risposta
        Intent replyIntent = new Intent(this, ReplyActivity.class);

        // Aggiungo l'intent alla coda
        PendingIntent replyPendingIntent = PendingIntent.getActivity(this, 0, replyIntent, 0);

        // Creo la notifica
        NotificationCompat.Builder replyNotificationBuilder = new NotificationCompat.Builder(this);
        replyNotificationBuilder.setSmallIcon(android.R.drawable.ic_btn_speak_now);
        replyNotificationBuilder.setContentTitle("Messaggio");
        replyNotificationBuilder.setContentText("Testo del messaggio");
        replyNotificationBuilder.setContentIntent(replyPendingIntent);
        replyNotificationBuilder.setNumber(++numMessages);
        replyNotificationBuilder.setAutoCancel(true);
        replyNotificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        replyNotificationBuilder.setVibrate(new long[]{1000, 1000});
        replyNotificationBuilder.setTicker("Hai una nuova notifica!");

        // Invio la notifica al notification manager
        mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(notificationID, replyNotificationBuilder.build());

        // Creo il remote input
        RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY).setLabel(getResources().getString(R.string.reply_label)).build();

        // Creo la notifica compact
        Notification replyNotification = new WearableNotifications.Builder(replyNotificationBuilder).addRemoteInputForContentIntent(remoteInput).build();

        // Passo la notifica appena creata al notification manager compat
        NotificationManagerCompat.from(this).notify(0, replyNotification);
    }

I hope that will be useful for other person.



来源:https://stackoverflow.com/questions/22630600/voice-input-with-android-wear-preview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!