gpt4 book ai didi

Turn on/off airplane mode by voice from Android api23+(通过Android api23+语音打开/关闭飞行模式)

转载 作者:bug小助手 更新时间:2023-10-25 18:41:20 42 4
gpt4 key购买 nike



To toggle flight mode programmatically should be possible. First need to create some voice interaction service. And then to have an activity that can submit a request to that service to know what the user wanted. And in the end flight mode can be enabled/disabled programmatically.

以编程方式切换飞行模式应该是可能的。首先需要创建一些语音交互服务。然后拥有一个可以向该服务提交请求以知道用户想要什么的活动。最后,可以通过编程启用/禁用飞行模式。


However the Android example of the voice interaction service is hard to understand and also it does not build in Android Studio (seems some modifications are needed).

然而,语音交互服务的Android示例很难理解,而且它也没有内置在Android Studio中(似乎需要进行一些修改)。


So I thought maybe someone could show how a minimal voice interaction service should be set up in order to be able to toggle airplane mode?

所以我想,也许有人可以演示一下,为了切换飞行模式,应该如何设置最低限度的语音交互服务?


MainActivity:

主要活动:


if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
VoiceInteractionSession voiceInteractionSession = new VoiceInteractionSession(this);
VoiceInteractor.PickOptionRequest.Option option1 = new VoiceInteractor.PickOptionRequest.Option("enable", 1);
option1.addSynonym("on");
VoiceInteractor.PickOptionRequest.Option option2 = new VoiceInteractor.PickOptionRequest.Option("disable", 1);
option1.addSynonym("off");
getVoiceInteractor().submitRequest(new VoiceInteractor.PickOptionRequest(new VoiceInteractor.Prompt("Flight mode?"), new VoiceInteractor.PickOptionRequest.Option[]{option1, option2}, null) {
@Override
public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
if (finished && selections.length == 1) {
selections[0].getIndex();
}
}

@Override
public void onCancel() {
getActivity().finish();
}
});

if (isVoiceInteraction()) {
Intent intent = new Intent(Settings.ACTION_VOICE_CONTROL_AIRPLANE_MODE);
intent.putExtra(Settings.EXTRA_AIRPLANE_MODE_ENABLED, true);
voiceInteractionSession.startVoiceActivity(intent);
}
}

VoiceInteractionService:

VoiceInteraction服务:


public class MyVoiceInteractionService extends VoiceInteractionService {
}

VoiceInteractionSessionService:

语音交互会话服务:


public class MyVoiceInteractionSessionService extends VoiceInteractionSessionService {
@Override
public VoiceInteractionSession onNewSession(Bundle bundle) {
return null;
}
}

RecognitionService:

RecognitionService:


public class MyRecognitionService extends RecognitionService {
@Override
protected void onStartListening(Intent intent, Callback callback) {
}

@Override
protected void onCancel(Callback callback) {
}

@Override
protected void onStopListening(Callback callback) {
}
}

AndroidManifest:

AndroidManifest:


<service android:name="MyVoiceInteractionService"
android:label="Test Voice Interaction Service"
android:permission="android.permission.BIND_VOICE_INTERACTION"
android:process=":interactor">
<meta-data android:name="android.voice_interaction"
android:resource="@xml/interaction_service" />
<intent-filter>
<action android:name="android.service.voice.VoiceInteractionService" />
</intent-filter>
</service>
<service android:name="MyVoiceInteractionSessionService"
android:permission="android.permission.BIND_VOICE_INTERACTION"
android:process=":session">
</service>
<service android:name="MyRecognitionService"
android:label="Test Voice Interaction Service">
<intent-filter>
<action android:name="android.speech.RecognitionService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.speech" android:resource="@xml/recognition_service" />
</service>

I have tried to run the code above, but as expected it cannot submit any request and in the end it cannot toggle flight mode (in the example code only trying to enable it).

我尝试运行上面的代码,但不出所料,它不能提交任何请求,最后也不能切换飞行模式(在本例中,代码只是试图启用它)。


更多回答

Hey, did you succeed to make it work?

嘿,你成功地让它成功了吗?

优秀答案推荐

I'm also looking for a method to toggle airplane mode without root using voice interaction.
The app should be the default assistant app of the system.
Try looking into this https://android.googlesource.com/platform/frameworks/base/+/marshmallow-release/tests/VoiceInteraction/src/com/android/test/voiceinteraction

我还在寻找一种方法,无需使用语音交互即可切换飞行模式。该应用程序应该是系统的默认助手应用程序。请尝试查看此https://android.googlesource.com/platform/frameworks/base/+/marshmallow-release/tests/VoiceInteraction/src/com/android/test/voiceinteraction


If you figure out how to make it work tell me.

如果你想出办法了,告诉我。


更多回答

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

正如它目前所写的,你的答案并不清楚。请编辑以添加更多详细信息,以帮助其他人了解这是如何解决提出的问题的。你可以在帮助中心找到更多关于如何写出好答案的信息。

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