Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

19.4. 通过 isNotification 判断是否需要执行 onStop() 和 onDestroy()

		
	private boolean isNotification = false;
	
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null) {
            boolean isExit = intent.getBooleanExtra("QUIT", false);
            if (isExit) {
                this.finish();
            }
            isNotification = intent.getBooleanExtra("Notification", false);
        }
        Log.d(TAG, "onNewIntent()");
    }
    
    @Override
    public void onStop() {
        super.onStop();
        if (isNotification) return;
        if (messageQueueService != null) {
            stopService(messageQueueService);
        }
        aigcSpeech.stopSpeech();
        Log.d(TAG, "onStop()");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isNotification) return;
        if (messageQueueService != null) {
            stopService(messageQueueService);
        }
        if (mainBroadcastReceiver != null) {
            unregisterReceiver(mainBroadcastReceiver);
        }
        aigcSpeech.stopSpeech();
        Log.d(TAG, "onDestroy()");
    }