0

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);
            }
        }
}
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
Abbas Ali
  • 41
  • 4
  • @Saket I noted in 'setting's running app' section that my service is using a process named 'com.example.finaleproject'. It is the package that has been imported in the above given service class. So when I close activity from background that process is killed and the service tries to restart two times and produces the above mentioned error two times. Please reply. – Abbas Ali May 25 '14 at 06:41
  • And my problem is something like this but this post doesn't provide solution. http://stackoverflow.com/questions/16651009/android-service-stops-when-app-is-closed – Abbas Ali May 25 '14 at 06:50
  • You need to look in your device's Logcat around the time you saw the "Unfortunately Call My Mob has stopped working" and find the stack trace of the error. There's no way for anyone to know what to look for without information like that. – Karakuri May 25 '14 at 07:12
  • @Karakuri Thank you so much for your comments. The problem is solved. I used onStartCommand() method and returned START_REDELIVER_INTENT from this method. Now the service restarts without showing any errors when activity is closed from background. Everything runs fine. – Abbas Ali May 25 '14 at 08:46
  • 1
    @Saket just to notify you about my above comment. Thank you so much. – Abbas Ali May 25 '14 at 08:47

0 Answers0