你好我正在使用android服务。我使用此示例Android Speech Recognition as a service on Android 4.1 & 4.2创建了一个服务,它工作正常,但现在我想发送一些数据,以便服务可以使用新设置运行,但我不知道如何做到这一点我已经使用了getExtra但是我无法使用以前的设置终止服务并以新设置开始。 这是我的活动代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id){
case R.id.timer_menu:{
if(item.isChecked()) item.setChecked(false);
else item.setChecked(true);
}
case R.id.t1:{
if (item.isChecked()) item.setChecked(false);
else {
Intent i = new Intent(this,MyService.class);
stopService(i);
i.putExtra("chk", 't');
i.putExtra("t", 30000);
startService(i);
item.setChecked(true);
}
}
case R.id.t2:{
if(item.isChecked()) item.setChecked(false);
else {
Intent i = new Intent(this,MyService.class);
stopService(i);
i.putExtra("chk", 't');
i.putExtra("t", 60000);
startService(i);
item.setChecked(true);
}
}
case R.id.speechmenu:{
if(item.isChecked()) item.setChecked(false);
else {
Intent i = new Intent(this,MyService.class);
stopService(i);
i.putExtra("chk", 's');
startService(i);
item.setChecked(true);
}
}
}
return super.onOptionsItemSelected(item);
}
public void StartMethod(View v)
{
Intent i = new Intent(this,MyService.class);
i.putExtra("chk", 't');
i.putExtra("t", 30000);
startService(i);
}
public void EndMethod(View v)
{
Intent i = new Intent(this,MyService.class);
stopService(i);
}
}
这是我的服务:
public class MyService extends Service{
long t;
char c;
protected static AudioManager mAudioManager;
protected SpeechRecognizer mSpeechRecognizer;
protected Intent mSpeechRecognizerIntent;
public static final String TAG = null;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate()
{
super.onCreate();
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(new SpeechRecognitionListener());
Toast.makeText(this, "Service created", Toast.LENGTH_SHORT).show();
Listening();
}
@Override
public int onStartCommand(Intent i, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
c=i.getCharExtra("chk", 't');
t=i.getLongExtra("t",30000);
return super.onStartCommand(i, flags, startId);
}
@Override
public void onDestroy()
{
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
protected class SpeechRecognitionListener implements RecognitionListener
{
@Override
public void onBeginningOfSpeech()
{
showToast();
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)
{
Log.d(TAG, "error = " + error); //$NON-NLS-1$
Listening();
}
@Override
public void onEvent(int eventType, Bundle params)
{
}
@Override
public void onPartialResults(Bundle partialResults)
{
}
@Override
public void onReadyForSpeech(Bundle params)
{
Log.d(TAG, "onReadyForSpeech"); //$NON-NLS-1$
}
@Override
public void onRmsChanged(float rmsdB)
{
}
@Override
public void onResults(Bundle arg0) {
// TODO Auto-generated method stub
String str = new String();
ArrayList<String> data = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (int i = 0; i < data.size(); i++)
{
str += data.get(i);
}
if(str.contains("hi")||str.contains("silent"))
{
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Toast.makeText(getBaseContext(), "Silent Mode Activated",
Toast.LENGTH_SHORT).show();
}
else if(str.contains("hello")||str.contains("normal"))
{
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Toast.makeText(getBaseContext(), "Normal Mode Activated",
Toast.LENGTH_SHORT).show();
}
if(c=='s')
{
Listening();
}
else if(c=='t')
{
Toast.makeText(getBaseContext(), "Mobile Mode will be switched automatically after"+ t,
Toast.LENGTH_SHORT).show();
timer.start();
Listening();
}
}
}
public CountDownTimer timer =new CountDownTimer(t,1000) {
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
@Override
public void onFinish() {
int ring=mAudioManager.getRingerMode();
if(ring==0) {
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Toast.makeText(getBaseContext(), "Normal Mode Activated",
Toast.LENGTH_SHORT).show();
}
else if(ring==2) {
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Toast.makeText(getBaseContext(), "Silent Mode Activated",
Toast.LENGTH_SHORT).show();
}
// TODO Auto-generated method stub
}
};
public void Listening(){
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
public void showToast(){
Toast.makeText(this, "Listening", Toast.LENGTH_SHORT).show();
}
}