Сервис перестает работать, когда приложение закрывается - PullRequest
2 голосов
/ 08 апреля 2019

Здесь я реализовал функциональность для чтения сообщений входящих push-уведомлений, используя Text-To-Speech. Я создал отдельный класс обслуживания для него, который работает нормально, и через некоторое время приходят уведомления, но он останавливает уведомления о прочтении.

public class TTSService extends Service implements TextToSpeech.OnInitListener {

private static final String TAG = "TTSService";
private String mSpeechMessage;
private TextToSpeech mTts;

@Override

public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public void onCreate() {

    mTts = new TextToSpeech(this,
            this  // OnInitListener
    );
    mTts.setSpeechRate(1f);
    super.onCreate();
}


@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    if (mTts != null) {
        mTts.stop();
        mTts.shutdown();
    }
    super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {
    if (intent != null && intent.hasExtra(AppConstant.FEEDBACK_MSG)) {
        mSpeechMessage = intent.getStringExtra(AppConstant.FEEDBACK_MSG);
    }

    speakMessage(mSpeechMessage);

    super.onStart(intent, startId);
}

@Override
public void onInit(int status) {
    Log.v(TAG, "oninit");
    if (status == TextToSpeech.SUCCESS) {
        int result = mTts.setLanguage(Locale.US);
        if (result == TextToSpeech.LANG_MISSING_DATA ||
                result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.v(TAG, "Language is not available.");
        } else {

            if (mSpeechMessage != null) {
                speakMessage(mSpeechMessage);
            }
        }
    } else {
        Log.v(TAG, "Could not initialize TextToSpeech.");
    }
}

private void speakMessage(String str) {
    if (str != null) {
        mTts.speak(str,
                TextToSpeech.QUEUE_FLUSH,
                null);
    }

    //Stop Service
    // stopSelf();
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    restartServiceIntent.setPackage(getPackageName());

    PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(
            AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + 1000,
            restartServicePendingIntent);

    super.onTaskRemoved(rootIntent);
}
}

И я запускаю этот сервис с FirebaseMessagingService как:

  private void startTTSService(String message) {
    Intent intent = new Intent(getApplicationContext(), TTSService.class);
    intent.putExtra(AppConstant.FEEDBACK_MSG, message);
    startService(intent);
  }

Было бы здорово, если кто-нибудь может помочь. Заранее спасибо.

Ответы [ 2 ]

2 голосов
/ 08 апреля 2019

На уровне API 26, Android ограничил доступ к фоновой службе.Вы можете решить эту проблему, запустив свой сервис в качестве переднего плана.

У меня была такая же проблема с моим единственным проектом, и я исправил ее, используя приведенный ниже код.

В YourService.class

private static final String NOTIFICATION_CHANNEL_ID_DEFAULT = "my_flow_notification_channel_default";

@Override
    public void onCreate() {
        super.onCreate();

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID_DEFAULT)
                .setOngoing(false).setSmallIcon(R.drawable.ic_notification).setPriority(Notification.PRIORITY_MIN);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID_DEFAULT,
                    NOTIFICATION_CHANNEL_ID_DEFAULT, NotificationManager.IMPORTANCE_LOW);
            notificationChannel.setDescription(NOTIFICATION_CHANNEL_ID_DEFAULT);
            notificationChannel.setSound(null, null);
            notificationManager.createNotificationChannel(notificationChannel);
            startForeground(1, builder.build());
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO: 
     return START_STICKY;
    }

Для запуска службы

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
     ContextCompat.startForegroundService(context, new Intent(context, YourService.class));
else
     context.startService(new Intent(context, YourService.class));

Для остановки службы

stopService(new Intent(getActivity(), YourService.class));

В вашем AndroidManifest.xml

Добавьте это разрешение

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

Добавьте это внутри тега.

<service
   android:name=".service.YourService"
   android:enabled="true"
   android:exported="true" />

Надеюсь, что это поможет ..:)

0 голосов
/ 08 апреля 2019

Вам лучше использовать IntentService для фоновых сервисов, если сервис не открыт. Следуйте этому документу IntentService , сохранить код:)

...