Android Voice Actions for Messaging

1.6k views Asked by At

I'm​ developing a small messaging app on my own and I was wondering if there is a possibility to allow the user to send messages using the Google Assistant. (Like WhatsApp and Google Allo)

Which API could I use to archive this?

2

There are 2 answers

6
Ayush Bansal On

Before going through the code please go through this links answer along with comments .

So as mentioned there are two types of actions that can be registered 1) Custom Action 2) System Action Here app has been registered with TAKE A NOTE action(a system generated action). This is registered in the Manifest to let the system know that it accepts the intents which are related to taking a note.(Hope i have not over explained it)

=== Code ===

Manifest.xml

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <action android:name="com.google.android.gms.actions.CREATE_NOTE" />
        <category android:name="android.intent.category.VOICE"/>
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain"/>
    </intent-filter>

MainActivity.java

public class VoiceAction extends AppCompatActivity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_voice_action);
         Intent t = getIntent();
         String s = t.getStringExtra(Intent.EXTRA_TEXT);
         System.out.println("====" + s);

     } }

This how i tested it

Ok Google -> take a note -> google asks me choose an app -> choose the app to be tested -> give a note or a text to it -> see your log (text you said is printed there).

I will edit the answer to meet your need exactly in some time . Hope this helps

0
Lowdev On

I saw something in the WhatsApp manifest :

Apparently they are using this action : com.google.android.voicesearch.SEND_MESSAGE_TO_CONTACTS

No trace of that in any docs... Coincidence, I think not...

https://github.com/GigaDroid/Decompiled-Whatsapp/blob/master/AndroidManifest.xml