简体   繁体   中英

can we invoke any activty or service on android wear using voice command

我知道我可以通过从磨损主屏幕调用语音命令(例如Start MyApp)开始我的应用程序启动器活动,但我想用相同的机制调用其他活动,即使用语音命令,例如start B,其中B是名称我的活动除了启动器活动。我想从Wear主屏幕本身启动Activty B而不是通过我的应用程序打开。类似于应用程序启动器活动的情况可以从Wear主屏幕启动。机器人磨损支持此功能?请帮忙

Yes it can be done.

@Override
protected void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);    
        voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        startActivityForResult(voiceIntent,SPEECH_RECOGNIZER_CODE);
        }

Start the activity as shown above , it'll open a default google voice listening dialogue box.

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   try { if (requestCode == SPEECH_RECOGNIZER_CODE && resultCode == RESULT_OK) {
            List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            String spokenText = results.get(0);
       }

Implement the above mentioned method in your activity. By matching the string "spokenText" to the Activity name you can start that activity.

你口头调用的活动会不时地改变,或者它总是第二次活动?

Well I can think of a crude fix, i assume that you have added "android:label" to your launcher activity so that the app is invoked by the voice command from the wear home screen.Add the label Value as "secondactivity"(Or the activity name you want to invoke - so that the user can use it without having to worry about the app name). Is it a problem if you finish the launcher activity in OnCreate method itself and open the second activity from there on ? Just my thoughts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM