WakefulIntentService无法实例化侦听器

时间:2014-01-26 12:26:03

标签: android commonsware-cwac

我发现我必须在xml文件中指定监听器,例如:

<WakefulIntentService listener="pathhere.MainListener" />

问题是我无法添加多个侦听器,所以我必须使用这种解决方法:

    public MainListener(ListenerTable listenerType) {
        this.listenerType = listenerType;
        System.out.println("Listener Type is " + listenerType);
    }

    public void scheduleAlarms(AlarmManager mgr, PendingIntent pi, Context ctxt) {
        System.out.println("Scheduling Alarm");
        if (listenerType == ListenerTable.CELL) {
            System.out.println("In scheduling cell");
            mgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                    SystemClock.elapsedRealtime() + 60000,
                    AlarmManager.INTERVAL_FIFTEEN_MINUTES / (15 * 60) * 10, pi);
        } else if (listenerType == ListenerTable.WIFI) {
            mgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                    SystemClock.elapsedRealtime() + 60000,
                    AlarmManager.INTERVAL_FIFTEEN_MINUTES / (15 * 60) * 60, pi);
        }

    }

    public void sendWakefulWork(Context ctxt) {

        switch (listenerType) {
        case CELL:
            WakefulIntentService.sendWakefulWork(ctxt, CellService.class);
            break;
        case WIFI:
            WakefulIntentService.sendWakefulWork(ctxt, WifiService.class);
            break;
        default:
            //Do nothing
            break;
        }
    }

我在主要活动中传递了计划警报:

WakefulIntentService.scheduleAlarms(new MainListener(ListenerTable.CELL),this, false);

现在问题是我得到一个运行时错误,该错误以:

开头
01-26 21:49:12.615: E/AndroidRuntime(8064): java.lang.RuntimeException: Unable to start receiver com.commonsware.cwac.wakeful.AlarmReceiver: java.lang.RuntimeException: Could not create instance of listener

我猜原因是因为它使用上面的xml文件并且没有将任何方法传递给构造函数。由于WakefulIntentService不支持多个侦听器,如何在WakefulIntentService内简单管理多个闹钟?

1 个答案:

答案 0 :(得分:0)

  

由于WakefulIntentService不支持多个侦听器,我如何在WakefulIntentService中简单地管理多个警报?

首先,请阅读the README section on asking questions,以便将来在StackOverflow问题上获得正确的标记。

然后,关注the "Basic Usage" instructions。我AlarmManager的帮助程序代码是为相当简单的场景设计的,但您不需要使用该代码。