📜 ⬆️ ⬇️

Voice control in Android applications



It all started with the fact that I looked a good review (comparison) of Siri and Google Now. Which of them is better, I will not argue, but I personally have a tablet on Android. I thought, what if I could write the calculator completely on voice control (would it be convenient?). But first I had to deal a little with the voice control itself, more precisely with voice input (control still needs to be achieved). In addition, I just downloaded Android Studio, and I could not wait to try it out in practice (well, on the mini-project). Well, let's start.


')
We throw on the activity ListView and Button . In ListVu, we will save the commands themselves, or rather the variants of one command, and the button will politely ask what we want. Yes, the program will not have logic, with its help we will just see the implementation itself.



Add the same resolution to the manifesto.
<uses-permission android:name="android.permission.INTERNET" /> 


And that's all, now you can go directly to programming. "Find the necessary components":

 private ListView mList; private Button speakButton; public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); speakButton = (Button) findViewById(R.id.button); speakButton.setOnClickListener(this); mList = (ListView) findViewById(R.id.listView); } 


We set the click handler for the button, which will call the startSpeak () method, which we will discuss later:

 public void onClick(View v) { startSpeak(); } 


Well, finally ended the "water". We start to "speak":
 public void startSpeak() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); //       () intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); //      intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "What can you tell me?"); startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); //    } 


It's time to give vent to fantasy and decide which teams to use. I myself first wanted to show on the example of a tetris tank: they would dictate to him “up”, “down”, “left”, “fire” and so on, but I leave it difficult for you. I gave commands to change the color of the button, exit the application, open the pages in the browser, launch the maps and reboot the device. As for the latter, reboot, this command will work, as I understand it, only on rooted devices. On the phone, I have the rights of a joint venture and everything works well, but on the tablet, it just ignores this command. There is nothing complicated in the record of commands, I think comments will be enough:

 public void onActivityResult(int requestCode, int resultCode, Intent data){ if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK){ ArrayList commandList = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, commandList)); //     ,    .     //            if (commandList.contains("red")){ speakButton.setText("red"); speakButton.setBackgroundColor(Color.RED); } if (commandList.contains("blue")){ speakButton.setText("blue"); speakButton.setBackgroundColor(Color.BLUE); } if (commandList.contains("green")){ speakButton.setText("green"); speakButton.setBackgroundColor(Color.GREEN); } if (commandList.contains("yellow")){ speakButton.setText("yellow"); speakButton.setBackgroundColor(Color.YELLOW); } if (commandList.contains("white")){ speakButton.setText("white"); speakButton.setBackgroundColor(Color.WHITE); } if (commandList.contains("black")){ speakButton.setText("black"); speakButton.setBackgroundColor(Color.BLACK); } //  if (commandList.contains("finish")){ finish(); } //     if (commandList.contains("maps")){ Intent i = new Intent(); PackageManager manager = getPackageManager(); i = manager.getLaunchIntentForPackage("com.google.android.apps.maps"); i.addCategory(Intent.CATEGORY_LAUNCHER); startActivity(i); } //    if (commandList.contains("google")){ finish(); Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); startActivity(browserIntent); } if (commandList.contains("facebook")){ finish(); Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com")); startActivity(browserIntent); } //       //  -   "android.permission.REBOOT",        () if (commandList.contains("reboot")){ try { Process proc = Runtime.getRuntime() .exec(new String[]{ "su", "-c", "reboot -p" }); proc.waitFor(); } catch (Exception ex) { ex.printStackTrace(); } } } super.onActivityResult(requestCode, resultCode, data); } 


This is the command entry window:



Say with a beautiful English accent "maps". Called Google Maps:



"Blue":



As you can see, the list displays all possible (similar) words and from them the necessary for us is chosen.

Well, on the "finish", I ended the conversation with a soulless (or not?) Machine.

I hope my small article will encourage someone to create (not, not a terminator) any promising project that will simplify people's daily lives, and you will bring millions, or at least it will be useful. Dare!

PS: a couple of useful links to the documentation and source

Source: https://habr.com/ru/post/180515/


All Articles