简体   繁体   中英

Start activity from google search or voice input

Task: do something in my app by command from android wear watch by speaking command. It the same as send command from google search (by speaking or writing command).

How-to described at http://developer.android.com/training/wearables/apps/voice.html "Declare App-provided Voice Actions"

I created two activities:

    <activity
        android:name=".MainActivity"
        android:label="speech test main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".SomeAction"
        android:label="speech test action" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Both activities can be launched from google search by command "start [android:label for activity]". Then I can do corresponding action at onResume().

But at system launcher icon is created for each activity! I tried to change category to DEFAULT, icon disappears, but activity cannot be launched from google search. Seems to be google search can launch activities with LAUNCHER category only.

So, need to launch activity (or just send intent, if possible) from google search. Problem - extra icons on launcher.

You cannot use <action android:name="android.intent.action.LAUNCHER" /> twice because that would give 2 launch icons. Also, remove <category android:name="android.intent.category.DEFAULT" /> from both activities. Instead, just focus on having only one main launcher activity where the getIntent() is called, then resend whatever intent/extras/flags/other data that came with the intent into your second activity if necessary. Alternatively, process the intent in the main launcher activity, then call another method in the second activity.

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