AlarmManager未调用后台服务

时间:2015-10-19 06:25:42

标签: android

我正在设置两个警报,它们将运行两个不同的后台服务。我在我的activity类的onCreate方法中设置了警报。但问题是扩展IntentService的服务类没有被调用,即他们的方法onHandleIntent()没有被调用。这就是我设置闹钟的方式

//Creating alarm for showing notifications.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        //create an alarm for today if there is still time else schedule alarm for tomorrow.(Handled inside the one time alarm class).
        //FIRST ALARM..............
        Intent intent = new Intent(ActionBarTabActivity.this, ScheduleOneTimeAlarmForToday.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        //SECOND ALARM.............
        Intent i = new Intent(ActionBarTabActivity.this,RemoteNotificationService.class);
        PendingIntent pi = PendingIntent.getService(getApplicationContext(), 111, i, 0);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), 1000 * 60, pi);

我已将minifest文件中的服务声明如下

    <service android:name="com.nss.common.services.ScheduleOneTimeAlarmForToday" />
    <service android:name="com.nss.common.services.RemoteNotificationService" />

此警报在我的旧三星手机上正常运行,但当我在我的新华硕Zenfone或任何其他新手机上进行测试时,它并没有显示出来。

编辑: 我的logcat显示了这个: 10-19 12:25:05.605 634-744/? V/AlarmManager﹕ triggered: flg=0x10000000 cmp=com.nss.zobbers/com.nss.common.services.ScheduleOneTimeAlarmForToday Pkg: com.nss.zobbers

10-19 12:25:06.846      634-744/? V/AlarmManager﹕ triggered: cmp=com.nss.zobbers/com.nss.common.services.RemoteNotificationService Pkg: com.nss.zobbers

所以我没有得到它,我的闹钟被触发但是它需要呼叫的服务没有被叫到?我尝试了很多帖子,但无法找到错误。请提前帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

我让它发挥作用,但我不接受它作为我的答案,也许有人会告诉它为什么不起作用的确切原因。好吧,我没有通过alarmManager直接调用服务,而是修改了我的代码来调用广播接收器,然后广播接收器调用相应的服务。现在它似乎在不同的设备上完美运行。