当app从后台关闭时,Android startService会出错

时间:2014-05-25 04:56:04

标签: android android-service

我在服务中实现语音识别。我为此目的使用startService()。当用户按下用户界面上的关闭按钮时,我停止此服务。问题是当我使用后退按钮关闭所有应用程序活动时,一个活动仍然在后台运行(任务管理器或最近的应用程序)。到目前为止,该服务运行良好。但是,当我从任务管理器或最近的应用程序关闭该活动时(我的服务仍在运行),会出现一个弹出窗口,显示"Unfortunately Call My Mob has stopped working"(Call My Mob是我的应用程序名称),此后服务自动停止错误。

我想要做的是在从任务管理器关闭活动时停止服务而不显示错误,或者继续运行服务而没有任何错误。我搜索了很多,但无法找到任何解决方案。我不知道什么是错的。任何帮助将受到高度赞赏。这是我的服务代码。

 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);
            }
        }
}

0 个答案:

没有答案