java.lang.IllegalStateException: Not allowed to start service Intent while trying to run RingtoneService

10,462

If you are running your code on Android 8.0 then this behavior is expected. Based on the documentation, starting from Android 8.0, you cannot start a service in background if your application is not in foreground. You need to replace following:

Intent serviceIntent = new Intent(context, RingtonePlayingService.class);
context.startService(serviceIntent);

Do

Intent serviceIntent = new Intent(context, RingtonePlayingService.class);
ContextCompat.startForegroundService(context, serviceIntent );

Ensure to call startForeground() in your onHandleIntent with notification. You can refer to this SO for details to implement it.

Share:
10,462
Pierre Ghaly
Author by

Pierre Ghaly

A Computer Science graduate, who did 3 years of Mechatronics Engineering before that then realised that's not my place. So I switched major since I am not a tree and never regretted it! It's never too late to do something you really love. So passionate about programming, it's not only my professional career it's also my major hobby besides reading and social activities/organisations/voluntary work. A night owl like most programmers I assume!

Updated on June 04, 2022

Comments

  • Pierre Ghaly
    Pierre Ghaly almost 2 years

    I am creating an organizer app which contains many functions one of them is an alarm, while trying to start the RingtoneService for my alarm most of the times I get this exception "java.lang.IllegalStateException: Not allowed to start service Intent" because it's running in the background (sometimes it runs with delay)! I extensively searched for an answer and tried the following and none worked: - JobScheduler : I get the same exception - bindService() and writing the code inside onServiceConnected() : it never hits the onServiceConnected()

    Below are the important parts of my code:

    public class AlarmReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(final Context context, Intent intent) {
    
            Intent serviceIntent = new Intent(context, RingtonePlayingService.class);
            context.startService(serviceIntent);
        }
    }
    

    Broadcast call from activity below:

    Intent intent = new Intent(AddAlarm.this, AlarmReceiver.class)
                    .putExtra("ALARM_ON", true);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    

    Service class below:

    public class RingtonePlayingService extends Service {
    
        // Player
        MediaPlayer player;
        boolean isRunning;
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    
            if (!isRunning) {
    
                player = MediaPlayer.create(this, R.raw.ringtone);
                player.start();
    
                this.isRunning = true;
    
                showNotification();
    
            }
            else if (isRunning) {
    
                player.stop();
    
                this.isRunning = false;
    
            }
    
            return START_STICKY;
        }
    }
    
  • Pierre Ghaly
    Pierre Ghaly about 6 years
    Thanks, that solved my problem! Intent serviceIntent = new Intent(context, RingtonePlayingService.class); context.startForegroundService(serviceIntent); and I called the startForeground() in onCreate() in my Service class instead of the onHandlerIntent and it works fine: public void onCreate() { super.onCreate(); initNotification(); startForeground(1, notificationBuilder.build()); }
  • Sagar
    Sagar about 6 years
    context.startForegroundService () is available from Android O. You need to either use ContextCompat or check version before calling it. You might have problem with older versions, since its not available in older versions. Remember to test on lower versions
  • Ali Kazi
    Ali Kazi almost 6 years
    This is not the optimal solution. You should not simply start bunch of long running foreground services. It is better to extend JobIntentService instead of Service and continue using startService which is a better solution.