原因:java.lang.IllegalStateException:

Caused by: java.lang.IllegalStateException:

我正面临这个问题,我不知道如何重现它。

Caused by: java.lang.IllegalStateException:

当我在 google Play 商店发布 2 小时后,我的应用程序在我的设备上运行良好,这些问题在 android vitals 的 崩溃报告 中产生. 我无法复制它。


java.lang.RuntimeException: 
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:3619)
  at android.app.ActivityThread.access00 (ActivityThread.java:237)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1803)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:214)
  at android.app.ActivityThread.main (ActivityThread.java:7091)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:975)
Caused by: java.lang.IllegalStateException: 
  at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1666)
  at android.app.ContextImpl.startService (ContextImpl.java:1611)
  at android.content.ContextWrapper.startService (ContextWrapper.java:677)
  at android.content.ContextWrapper.startService (ContextWrapper.java:677)
  at b.k.a.a.b (Unknown Source:22)
  at sjtechnologies.muslimapp.service.PrayerAlarm.onReceive (Unknown Source:21)
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:3610)

我的祈祷Alram代码


public class PrayerAlarm extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle b = intent.getExtras();
        startWakefulService(context, new Intent(context, PrayerNotification.class).putExtra("prayName", b.getString("prayName")));

        Log.i("ACTIVITY_SRAT" , "PrayerAlarm is working well");
    }
}


祈祷通知class 在这里。我不知道我错过了什么。也许 wakefullservice 创建问题


public class PrayerNotification extends Service {
    private String prayingName , prayerType;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        prayingName = intent.getStringExtra("prayName");
        //not mid night
        Log.i("ACTIVITY_SRAT" , "PrayerNotification is working well");
        if (ConfigPreferences.getPrayingNotification(this) && !prayingName.equals("6"))
            showNotification();
        if (ConfigPreferences.getSilentMood(this)) changeMobileToSilentMood();
        sendBroadcast(new Intent().setAction("prayer.information.change"));
        stopSelf();
        PrayerAlarm.completeWakefulIntent(intent);
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * Function to check to make mobile silent in prayer
     */
    private void changeMobileToSilentMood() {
        AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int ringerMode = mAudioManager.getRingerMode();
        if (ringerMode != AudioManager.RINGER_MODE_SILENT) {
            Alarms.switchToSilent(10, this);
        }
    }

    /**
     * Function to show prayer notification
     */
    public void showNotification() {

        switch (prayingName) {
            case "0":
                prayingName = this.getString(R.string.fajr_prayer);
                prayerType = PrayerImageActivity.MOSQUE_NIGHT;
                AppLog.add(prayingName);
                break;
            case "1":
                prayingName = this.getString(R.string.sunrize_prayer);
                prayerType = PrayerImageActivity.MOSQUE_DAY;
                AppLog.add(prayingName);
                break;
            case "2":
                HGDate hgDate = new HGDate();
                prayingName = hgDate.weekDay() != 5 ? this.getString(R.string.zuhr_prayer) : this.getString(R.string.jomma_prayer);

                prayerType = PrayerImageActivity.MOSQUE_DAY;
                AppLog.add(prayingName);
                break;
            case "3":
                prayingName = this.getString(R.string.asr_prayer);
                prayerType = PrayerImageActivity.MOSQUE_DAY;
                AppLog.add(prayingName);
                break;
            case "4":
                prayingName = this.getString(R.string.magreb_prayer);
                prayerType = PrayerImageActivity.MOSQUE_NIGHT;
                AppLog.add(prayingName);
                break;
            case "5":
                prayingName = this.getString(R.string.asha_prayer);
                prayerType = PrayerImageActivity.MOSQUE_NIGHT;
                AppLog.add(prayingName);
                break;
            case "6":
                prayingName = this.getString(R.string.mid_night);
                AppLog.add(prayingName);
                break;

        }


        NotificationCompat.Builder builder;
        boolean aboveLollipopFlag = android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
        PendingIntent intent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), 0);
        if (ConfigPreferences.getLedNotification(this)) {
            builder = new NotificationCompat.Builder(this).
                    setSmallIcon(aboveLollipopFlag ? R.drawable.notification_white : R.drawable.icon)
                    .setPriority(Notification.PRIORITY_MAX)
                    .setContentText(prayingName)
                    .setContentTitle(getString(R.string.remember))
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .setLights(0xFF00ff00, 1000, 1000)
                    .setAutoCancel(true)
                    .setColor(Color.parseColor("#2a5f54"))
                    .setContentIntent(intent);
        } else {
            builder = new NotificationCompat.Builder(this).
                    setSmallIcon(aboveLollipopFlag ? R.drawable.notification_white : R.drawable.icon)
                    .setPriority(Notification.PRIORITY_MAX)
                    .setContentText(prayingName)
                    .setContentTitle(getString(R.string.remember))
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .setAutoCancel(true)
                    .setColor(Color.parseColor("#2a5f54"))
                    .setContentIntent(intent);
        }

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());

        /*For start activities when prayer come*/

//        Intent intent1 = new Intent(this , PrayerImageActivity.class);
//        Log.i("ACTIVITY_SRAT" , "Start Activity is working well");
//        intent1.putExtra(PrayerImageActivity.MOSQUE_TYPE , prayerType);
//        intent1.putExtra(PrayerImageActivity.PRAY_TYPE , prayingName);
//        startActivity(intent1);


    }

    boolean isShow = true;
    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }
}

文档表明 IllegalStateException 在以下情况下抛出:

If the application is in a state where the service can not be started (such as not in the foreground in a state when services are allowed).

这意味着当您的 BroadcastReceiver 被触发时,您的应用程序仍在后台。