【问题标题】:java.lang.IllegalStateException: Not allowed to start service Intent while trying to run RingtoneServicejava.lang.IllegalStateException:尝试运行 RingtoneService 时不允许启动服务 Intent
【发布时间】:2018-10-02 07:55:00
【问题描述】:

我正在创建一个包含许多功能的管理器应用程序,其中一个是警报,同时尝试为我的警报启动 RingtoneService 大多数时候我收到此异常“java.lang.IllegalStateException: Not allowed to start service Intent " 因为它在后台运行(有时它会延迟运行)! 我广泛搜索了答案并尝试了以下方法,但均未奏效: - JobScheduler:我得到了同样的例外 - bindService() 并在 onServiceConnected() 中编写代码:它永远不会碰到 onServiceConnected()

以下是我的代码的重要部分:

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {

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

来自以下活动的广播呼叫:

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

下面的服务类:

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

【问题讨论】:

  • 如果您在 Android 8.0+ 上获得此功能,则需要切换到 startForegroundService() 并让您的服务调用 startForeground()
  • @PeeGee 您使用哪个操作系统版本来测试它?
  • @CommonsWare,谢谢,解决了我的问题
  • @Sagar 我使用的是 Android 8.0,这就是我遇到的问题

标签: android android-studio exception service android-mediaplayer


【解决方案1】:

如果您在 Android 8.0 上运行代码,那么这种行为是正常的。基于documentation,从Android 8.0 开始,如果您的应用程序不在前台,您将无法在后台启动服务。您需要替换以下内容:

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

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

确保在您的 onHandleIntent 中调用 startForeground() 并发出通知。实现细节可以参考这个SO

【讨论】:

  • 谢谢,这解决了我的问题! Intent serviceIntent = new Intent(context, RingtonePlayingService.class); context.startForegroundService(serviceIntent); 我在我的 Service 类中调用了 onCreate() 中的 startForeground() 而不是 onHandlerIntent 并且它工作正常:public void onCreate() { super.onCreate(); initNotification(); startForeground(1, notificationBuilder.build()); }
  • context.startForegroundService () 可从 Android O 获得。您需要使用 ContextCompat 或在调用它之前检查版本。您可能对旧版本有疑问,因为它在旧版本中不可用。记得在低版本上测试
  • 这不是最佳解决方案。您不应该简单地启动一堆长时间运行的前台服务。最好扩展JobIntentService 而不是Service 并继续使用startService,这是一个更好的解决方案。
猜你喜欢
  • 2018-03-08
  • 2019-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
相关资源
最近更新 更多