gpt4 book ai didi

java - 我的应用程序中的 Google Now 服务

转载 作者:行者123 更新时间:2023-11-30 11:20:56 24 4
gpt4 key购买 nike

我被某项服务和 google TTS 阻止了。好吧,我希望某项服务在 bg 中启动,当我说“ok google”或我选择的任何内容时,启动文本识别的 Intent 。我创建了服务,但现在……我该怎么办?我卡住了。这是服务:

public class GnowService extends Service
{
protected static AudioManager mAudioManager;
protected SpeechRecognizer mSpeechRecognizer;
protected Intent mSpeechRecognizerIntent;
protected final Messenger mServerMessenger = new Messenger(new IncomingHandler(this));

protected boolean mIsListening;
protected volatile boolean mIsCountDownOn;
private static boolean mIsStreamSolo;

static final int MSG_RECOGNIZER_START_LISTENING = 1;
static final int MSG_RECOGNIZER_CANCEL = 2;

@Override
public void onCreate()
{
super.onCreate();
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(new SpeechRecognitionListener());
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
}

protected static class IncomingHandler extends Handler
{
private WeakReference<GnowService> mtarget;

IncomingHandler(GnowService target)
{
mtarget = new WeakReference<GnowService>(target);
}


@Override
public void handleMessage(Message msg)
{
final GnowService target = mtarget.get();

switch (msg.what)
{
case MSG_RECOGNIZER_START_LISTENING:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
// turn off beep sound
if (!mIsStreamSolo)
{
mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
mIsStreamSolo = true;
}
}
if (!target.mIsListening)
{
target.mSpeechRecognizer.startListening(target.mSpeechRecognizerIntent);
target.mIsListening = true;
//Log.d(TAG, "message start listening"); //$NON-NLS-1$
}
break;

case MSG_RECOGNIZER_CANCEL:
if (mIsStreamSolo)
{
mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, false);
mIsStreamSolo = false;
}
target.mSpeechRecognizer.cancel();
target.mIsListening = false;
//Log.d(TAG, "message canceled recognizer"); //$NON-NLS-1$
break;
}
}
}

// Count down timer for Jelly Bean work around
protected CountDownTimer mNoSpeechCountDown = new CountDownTimer(5000, 5000)
{

@Override
public void onTick(long millisUntilFinished)
{
// TODO Auto-generated method stub

}

@Override
public void onFinish()
{
mIsCountDownOn = false;
Message message = Message.obtain(null, MSG_RECOGNIZER_CANCEL);
try
{
mServerMessenger.send(message);
message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);
mServerMessenger.send(message);
}
catch (RemoteException e)
{

}
}
};

@Override
public void onDestroy()
{
super.onDestroy();

if (mIsCountDownOn)
{
mNoSpeechCountDown.cancel();
}
if (mSpeechRecognizer != null)
{
mSpeechRecognizer.destroy();
}
}

protected class SpeechRecognitionListener implements RecognitionListener
{

@Override
public void onBeginningOfSpeech()
{
// speech input will be processed, so there is no need for count down anymore
if (mIsCountDownOn)
{
mIsCountDownOn = false;
mNoSpeechCountDown.cancel();
}
//Log.d(TAG, "onBeginingOfSpeech"); //$NON-NLS-1$
}

@Override
public void onBufferReceived(byte[] buffer)
{

}

@Override
public void onEndOfSpeech()
{
//Log.d(TAG, "onEndOfSpeech"); //$NON-NLS-1$
}

@Override
public void onError(int error)
{
if (mIsCountDownOn)
{
mIsCountDownOn = false;
mNoSpeechCountDown.cancel();
}
mIsListening = false;
Message message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);
try
{
mServerMessenger.send(message);
}
catch (RemoteException e)
{

}
//Log.d(TAG, "error = " + error); //$NON-NLS-1$
}

@Override
public void onEvent(int eventType, Bundle params)
{

}

@Override
public void onPartialResults(Bundle partialResults)
{

}

@Override
public void onReadyForSpeech(Bundle params)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
mIsCountDownOn = true;
mNoSpeechCountDown.start();

}
Log.d("", "onReadyForSpeech"); //$NON-NLS-1$
}

@Override
public void onResults(Bundle results)
{
//Log.d(TAG, "onResults"); //$NON-NLS-1$

}

@Override
public void onRmsChanged(float rmsdB)
{

}

}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}

我必须在我的 MainActivity 中创建一些东西吗?我怎样才能“告诉”应用程序识别例如“ok google”这样的词?开始的 Intent ?谢谢编辑:

这是我在 list 中集成的部分:

<receiver
android:name=".mBootCompletedReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />

</intent-filter>
</receiver>
<service android:name=".GnowService"></service>

在我的 MainActivity 中我创建了 Broadcastreceiver

private BroadcastReceiver mBootCompletedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.dd.speech.GnowService");
context.startService(serviceIntent);
}
};

首先..到目前为止是正确的吗?还是我错了?然后,其他的点我不知道我是否理解,我是否能够做到。您有相应的示例代码吗?

最佳答案

在我的回答中提出解决方案,识别器总是会倾听。在说出关键字之前,它什么都不做。假设应用在用户说“开始结束游戏”之前不执行任何操作,并且在用户说“停止结束游戏”时再次不执行任何操作。

  1. 在 list 中注册 Intent.ACTION_BOOT_COMPLETED 并创建一个 BroadcastReceiver 类来监听此事件并启动服务。您的应用程序必须至少启动一次,接收器才能接收此广播。
  2. 在您的 MainActivity 中启动 onCreate 中的服务。如果要与服务器通信,则在 onStart 中绑定(bind)到服务,在 onStop 中解除绑定(bind)。
  3. 在服务中,创建一个标志 private boolean mShouldProcessResult;
  4. 在 onResults 中,如果 mShouldProcessResult 标志为假,则只需检查结果是否包含“开始结束游戏”。如果是,请向用户表明该应用程序将开始处理他/她所说的话并将 mShouldProcessResult 标志设置为 true,否则什么都不做。如果 mShouldProcessResult 标志为真,则处理用户所说的一切,这应该包括“停止结束游戏”。

请注意,电池生命周期会大大缩短,在某些情况下会缩短一半。

关于java - 我的应用程序中的 Google Now 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22485714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com