造成原因:java.lang.IllegalStateException:

时间:2019-11-19 20:31:32

标签: android android-intent broadcastreceiver android-service android-broadcast

我正面临这个问题,我不知道该如何复制它。

  

原因:java.lang.IllegalStateException:

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


java.lang.RuntimeException: 
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:3619)
  at android.app.ActivityThread.access$1300 (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");
    }
}


此处是“祈祷通知”类。我不知道我缺少什么。也许是唤醒服务造成问题


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

1 个答案:

答案 0 :(得分:1)

文档记录表明在以下情况下引发了IllegalStateException

  

如果应用程序处于无法启动服务的状态   (例如在允许服务的状态下不在前台)。

这意味着BroadcastReceiver触发时,您的应用仍处于后台。