下面列出了怎么用android.app.VoiceInteractor的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public IVoiceInteractorRequest startConfirmation(String callingPackage,
IVoiceInteractorCallback callback, VoiceInteractor.Prompt prompt, Bundle extras) {
ConfirmationRequest request = new ConfirmationRequest(callingPackage,
Binder.getCallingUid(), callback, VoiceInteractionSession.this,
prompt, extras);
addRequest(request);
mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageO(MSG_START_CONFIRMATION,
request));
return request.mInterface;
}
@Override
public IVoiceInteractorRequest startPickOption(String callingPackage,
IVoiceInteractorCallback callback, VoiceInteractor.Prompt prompt,
VoiceInteractor.PickOptionRequest.Option[] options, Bundle extras) {
PickOptionRequest request = new PickOptionRequest(callingPackage,
Binder.getCallingUid(), callback, VoiceInteractionSession.this,
prompt, options, extras);
addRequest(request);
mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageO(MSG_START_PICK_OPTION,
request));
return request.mInterface;
}
@Override
public IVoiceInteractorRequest startCompleteVoice(String callingPackage,
IVoiceInteractorCallback callback, VoiceInteractor.Prompt message, Bundle extras) {
CompleteVoiceRequest request = new CompleteVoiceRequest(callingPackage,
Binder.getCallingUid(), callback, VoiceInteractionSession.this,
message, extras);
addRequest(request);
mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageO(MSG_START_COMPLETE_VOICE,
request));
return request.mInterface;
}
@Override
public IVoiceInteractorRequest startAbortVoice(String callingPackage,
IVoiceInteractorCallback callback, VoiceInteractor.Prompt message, Bundle extras) {
AbortVoiceRequest request = new AbortVoiceRequest(callingPackage,
Binder.getCallingUid(), callback, VoiceInteractionSession.this,
message, extras);
addRequest(request);
mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageO(MSG_START_ABORT_VOICE,
request));
return request.mInterface;
}
PickOptionRequest(String packageName, int uid, IVoiceInteractorCallback callback,
VoiceInteractionSession session, VoiceInteractor.Prompt prompt,
VoiceInteractor.PickOptionRequest.Option[] options, Bundle extras) {
super(packageName, uid, callback, session, extras);
mPrompt = prompt;
mOptions = options;
}
void sendPickOptionResult(boolean finished,
VoiceInteractor.PickOptionRequest.Option[] selections, Bundle result) {
try {
if (DEBUG) Log.d(TAG, "sendPickOptionResult: req=" + mInterface
+ " finished=" + finished + " selections=" + selections
+ " result=" + result);
if (finished) {
finishRequest();
}
mCallback.deliverPickOptionResult(mInterface, finished, selections, result);
} catch (RemoteException e) {
}
}
void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
super.dump(prefix, fd, writer, args);
writer.print(prefix); writer.print("mPrompt=");
writer.println(mPrompt);
if (mOptions != null) {
writer.print(prefix); writer.println("Options:");
for (int i=0; i<mOptions.length; i++) {
VoiceInteractor.PickOptionRequest.Option op = mOptions[i];
writer.print(prefix); writer.print(" #"); writer.print(i); writer.println(":");
writer.print(prefix); writer.print(" mLabel=");
writer.println(op.getLabel());
writer.print(prefix); writer.print(" mIndex=");
writer.println(op.getIndex());
if (op.countSynonyms() > 0) {
writer.print(prefix); writer.println(" Synonyms:");
for (int j=0; j<op.countSynonyms(); j++) {
writer.print(prefix); writer.print(" #"); writer.print(j);
writer.print(": "); writer.println(op.getSynonymAt(j));
}
}
if (op.getExtras() != null) {
writer.print(prefix); writer.print(" mExtras=");
writer.println(op.getExtras());
}
}
}
}
private void startVoiceTrigger() {
Log.d(TAG, "startVoiceTrigger: ");
Option option = new Option("cheese", 1);
option.addSynonym("ready");
option.addSynonym("go");
option.addSynonym("take it");
option.addSynonym("ok");
VoiceInteractor.Prompt prompt = new VoiceInteractor.Prompt("Say Cheese");
getActivity().getVoiceInteractor()
.submitRequest(new PickOptionRequest(prompt, new Option[]{option}, null) {
@Override
public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
if (finished && selections.length == 1) {
Message message = Message.obtain();
message.obj = result;
takePicture();
} else {
getActivity().finish();
tearDown();
}
}
@Override
public void onCancel() {
getActivity().finish();
tearDown();
}
});
}
ConfirmationRequest(String packageName, int uid, IVoiceInteractorCallback callback,
VoiceInteractionSession session, VoiceInteractor.Prompt prompt, Bundle extras) {
super(packageName, uid, callback, session, extras);
mPrompt = prompt;
}
/**
* Return the prompt informing the user of what they are picking, as per
* {@link android.app.VoiceInteractor.PickOptionRequest VoiceInteractor.PickOptionRequest}.
*/
@Nullable
public VoiceInteractor.Prompt getVoicePrompt() {
return mPrompt;
}
CompleteVoiceRequest(String packageName, int uid, IVoiceInteractorCallback callback,
VoiceInteractionSession session, VoiceInteractor.Prompt prompt, Bundle extras) {
super(packageName, uid, callback, session, extras);
mPrompt = prompt;
}
AbortVoiceRequest(String packageName, int uid, IVoiceInteractorCallback callback,
VoiceInteractionSession session, VoiceInteractor.Prompt prompt, Bundle extras) {
super(packageName, uid, callback, session, extras);
mPrompt = prompt;
}
/**
* Return the message informing the user of the problem, as per
* {@link android.app.VoiceInteractor.AbortVoiceRequest VoiceInteractor.AbortVoiceRequest}.
*/
@Nullable
public VoiceInteractor.Prompt getVoicePrompt() {
return mPrompt;
}
/**
* Retrieve the active {@link VoiceInteractor} that the user is going through to
* interact with this activity.
*/
@Override
public VoiceInteractor getVoiceInteractor() {
return super.getVoiceInteractor();
}
/**
* Return the prompt informing the user of what will happen, as per
* {@link android.app.VoiceInteractor.ConfirmationRequest
* VoiceInteractor.ConfirmationRequest}.
*/
@Nullable
public VoiceInteractor.Prompt getVoicePrompt() {
return mPrompt;
}
/**
* Return the set of options the user is picking from, as per
* {@link android.app.VoiceInteractor.PickOptionRequest VoiceInteractor.PickOptionRequest}.
*/
public VoiceInteractor.PickOptionRequest.Option[] getOptions() {
return mOptions;
}
/**
* Report an intermediate option selection from the request, without completing it (the
* request is still active and the app is waiting for the final option selection),
* resulting in a call to
* {@link android.app.VoiceInteractor.PickOptionRequest#onPickOptionResult
* VoiceInteractor.PickOptionRequest.onPickOptionResult} with false for finished.
*/
public void sendIntermediatePickOptionResult(
VoiceInteractor.PickOptionRequest.Option[] selections, Bundle result) {
sendPickOptionResult(false, selections, result);
}
/**
* Report the final option selection for the request, completing the request
* and resulting in a call to
* {@link android.app.VoiceInteractor.PickOptionRequest#onPickOptionResult
* VoiceInteractor.PickOptionRequest.onPickOptionResult} with false for finished.
* This finishes the request (it is no longer active).
*/
public void sendPickOptionResult(
VoiceInteractor.PickOptionRequest.Option[] selections, Bundle result) {
sendPickOptionResult(true, selections, result);
}
/**
* Return the message informing the user of the completion, as per
* {@link android.app.VoiceInteractor.CompleteVoiceRequest
* VoiceInteractor.CompleteVoiceRequest}.
*/
@Nullable
public VoiceInteractor.Prompt getVoicePrompt() {
return mPrompt;
}
VoiceInteractor getVoiceInteractor();
VoiceInteractor super_getVoiceInteractor();