2015-10-19 85 views
0

我想設置兩個報警,它將運行兩個不同的後臺服務。我在我的activity類的onCreate方法中設置了警報。但問題是擴展IntentService的服務類沒有被調用,即他們的方法onHandleIntent()沒有被調用。這是我怎麼設置我的報警AlarmManager不調用後臺服務

//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 

所以我不明白這一點,我的報警被觸發,但它需要調用該服務不會被調用?我嘗試了很多帖子,但找不到錯誤。請提前幫助,謝謝。

回答

0

我讓它工作,但我不會接受它作爲我的答案,也許有人會告訴它爲什麼不起作用的確切原因。那麼,我不是通過alarmManager直接調用服務,而是修改我的代碼來調用廣播接收器,然後調用相應的服務。現在它似乎可以在不同的設備上完美運行。