It is not under singleton mode, U can't use it

时间秒杀一切 提交于 2020-02-04 02:41:58

问题


In my Reminder app, If I select time which just passed away, then app not working, getting following error:

Log

D/ANRAppManager: !!! It is not under singleton mode, U can't use it. !!!
05-13 10:55:19.934 22594-22600/com.mri I/dalvikvm: threadid=3: reacting to signal 3
05-13 10:55:20.190 22594-22600/com.mri D/dalvikvm: JIT unchain all for threadid=1
05-13 10:55:20.940 22594-22600/com.mri W/dalvikvm: threadid=3: spin on suspend #1 threadid=1 (pcf=0)
05-13 10:55:20.940 22594-22600/com.mri E/dalvikvm: Fatal spin-on-suspend, dumping threads
05-13 10:55:20.940 22594-22600/com.mri I/dalvikvm: DALVIK THREADS:
05-13 10:55:20.940 22594-22600/com.mri I/dalvikvm: (mutexes: tll=1 tsl=1 tscl=0 ghl=0)
05-13 10:55:20.940 22594-22600/com.mri I/dalvikvm: "main" prio=5 tid=1 RUNNABLE JIT
05-13 10:55:20.940 22594-22600/com.mri I/dalvikvm:   | group="main" sCount=1 dsCount=0 obj=0x4191cde0 self=0x418328b8
05-13 10:55:20.940 22594-22600/com.mri I/dalvikvm:   | sysTid=22594 nice=0 sched=0/0 cgrp=apps handle=1074880900
05-13 10:55:20.941 22594-22600/com.mri I/dalvikvm:   | state=R schedstat=( 133450832707 8412096123 56516 ) utm=13019 stm=326 core=0
05-13 10:55:20.941 22594-22600/com.mri I/dalvikvm:     at com.mri.reminders.AlarmReceiver.setRepeatAlarm(AlarmReceiver.java:~115)
05-13 10:55:20.941 22594-22600/com.mri I/dalvikvm:     at com.mri.reminders.ReminderAddActivity.saveReminder(ReminderAddActivity.java:963)
05-13 10:55:20.941 22594-22600/com.mri I/dalvikvm:     at com.mri.reminders.ReminderAddActivity.onOptionsItemSelected(ReminderAddActivity.java:1172)
05-13 10:55:20.942 22594-22600/com.mri I/dalvikvm:     at android.app.Activity.onMenuItemSelected(Activity.java:2633)
05-13 10:55:20.945 22594-22600/com.mri I/dalvikvm:     at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
05-13 10:55:20.945 22594-22600/com.mri I/dalvikvm:     at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147)
05-13 10:55:20.945 22594-22600/com.mri I/dalvikvm:     at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)

Here is the code which I am using in AlarmReceiver.java

public void setRepeatAlarm(Context context, Calendar calendar, int ID, long RepeatTime) {
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        // Put Reminder ID in Intent Extra
        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
        mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        // Calculate notification timein
        Calendar c = Calendar.getInstance();
        long currentTime = c.getTimeInMillis();
        long diffTime = calendar.getTimeInMillis() - currentTime;

        while(diffTime < 0) {
             diffTime += RepeatTime; // at this line getting error
        }

        // Start alarm using initial notification time and repeat interval time
        mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + diffTime,
                RepeatTime , mPendingIntent);

        // Restart alarm if device is rebooted
        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }

How can I resolve this issue ?


回答1:


I think that reacting to signal 3 is a symptom of an ANR. Does the application hang before crashing ? If so, it may be because the loops never ends.

Are you sure that RepeatTime is always > 0 (and specially not = 0) ?

Something like :

   while(diffTime < 0) {
         Log.d("SOME_TAG", "set diffTime to " + diffTime);
         diffTime += RepeatTime; // at this line getting error
    }

should not display more than a few lines.

Update :

You should ensure that either the alarm is in the future or the repeat time is greater than 0.

    if (diffTime > 0 || RepeatTime > 0) {

        // set the alarm only if the parameters are consistents

        while(diffTime < 0) {
             diffTime += RepeatTime; // at this line getting error
        }

        // Start alarm using initial notification time and repeat interval time
        mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + diffTime,
                RepeatTime , mPendingIntent);

        // Restart alarm if device is rebooted
        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

    }


来源:https://stackoverflow.com/questions/37201956/it-is-not-under-singleton-mode-u-cant-use-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!