gpt4 book ai didi

android - Android Wear Preview 的语音输入

转载 作者:行者123 更新时间:2023-11-29 17:51:21 25 4
gpt4 key购买 nike

我正在尝试使用新库来开发带有可穿戴设备(Android Wear)的 Android 应用程序。我了解该库的工作原理,现在我正在尝试为我在 this tutorial 中找到的代码开发一个通知,但是当我用一个简单的按钮模拟一个通知时,它什么也没做,我不知道为什么......我在这里发布我的代码我希望你能帮助我理解为什么它不起作用:

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();

}

我直接从 Android 开发人员页面按照教程操作,但它不起作用。我发布的代码有什么问题?

最佳答案

我使用以下代码解决了问题:

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);
}

我希望这对其他人有用。

关于android - Android Wear Preview 的语音输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22630600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com