I'm implementing speech recognition in a service. I'm using startService()
for this purpose. I stop this service when user presses the off button on user interface. The problem is when I close all the application activities using the back button, one activity still runs in the background (task manager or recent apps). Up until this point, the service runs fine. However, when I close that activity from task manager or recent apps, (my service is still running), a pop up window appears showing "Unfortunately Call My Mob has stopped working"
(Call My Mob is my app name) and the service is automatically stopped after this error.
What I want to do is either stop the service without showing an error when the activity is closed from the task manager or continue running the service without any errors. I searched a lot but couldn't find any solution. I don't know what's wrong. Any help will be highly appreciated. Here is my service code.
public class MyService extends Service {
Intent myintent;
Intent curr_intent;
Intent ring;
String name;
String str = new String();
SpeechRecognizer sr;
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();
sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(new listener());
myintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
myintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
ring = new Intent("android.intent.action.RING");
ring.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_PLAY_SOUND);
}
@Override
public void onStart(Intent intent, int startId) {
// For time consuming an long tasks you can launch a new thread here...
curr_intent = intent;
name = curr_intent.getStringExtra("phonename");
ring.putExtra("phonename", name);
Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();
sr.startListening(myintent);
}
@Override
public void onDestroy() {
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
class listener implements RecognitionListener {
public void onReadyForSpeech(Bundle params) {
}
public void onBeginningOfSpeech() {
Toast t = Toast.makeText(getApplicationContext(),
"On Beginning of Speech",
Toast.LENGTH_SHORT);
t.show();
}
public void onRmsChanged(float rmsdB) {
}
public void onBufferReceived(byte[] buffer) {
sr.startListening(myintent);
}
public void onEndOfSpeech() {
}
public void onError(int error) {
sr.startListening(myintent);
}
public void onResults(Bundle results) {
ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (int i = 0; i < data.size(); i++)
str += data.get(i);
if(str.equals(name))
startActivity(ring);
else
sr.startListening(myintent);
}
public void onPartialResults(Bundle partialResults) {
sr.startListening(myintent);
}
public void onEvent(int eventType, Bundle params) {
sr.startListening(myintent);
}
}
}