2015-04-08 5 views
0

Я написал несколько простых классов для расписание запуска службы и дата окончания.Служба планирования с диспетчером аварийных сообщений

я получил следующее:

  • ServiceTest: простой сервис, регистрация OnStart, OnDestroy и работает.
  • ServiceSchedulerTest: начало календарного планирования ServiceTest и дата окончания , используя AlarmManager
  • ServiceStopBroadcastReceiverTest: BroadcasteReceiver для остановки службы.
  • ScheduleUnit: Запись для хранения даты начала и окончания для планирования.

Я проверил мое обслуживание с простым вызова:

Intent intent = new Intent(this, ServiceTest.class); 
    startService(intent); 

И это работает отлично ... Однако, я не могу сделать планирование работы с менеджером сигнализации.

Вот классы:

ScheduleUnit:

public class ScheduleUnit { 

public String id; 
public Date dateStart, dateEnd; // Start and end date for a scheduling 

public ScheduleUnit(String id, String dateStart, String dateEnd) { 
    this.id = id; 
    this.dateStart = stringToDate(dateStart); 
    this.dateEnd = stringToDate(dateEnd); 
} 

public Date stringToDate(String dateStr) { 

    Date date = null; 
    try { 
     date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 
    return date; 
} 
} 

ServiceTest:

/** 
* 
* Simple test service, logging starting, running and destroying. 
*/ 

public class ServiceTest extends Service { 

private final String TAG = "MyDebug_Service_Test"; 
private final boolean DEBUG = true; 

private Handler handler; 
private Runnable runnable = new Runnable() { 

    @Override 
    public void run() { 
     log("Service is actually still alive."); 

     handler.postDelayed(this, 5000); 
    } 

}; 

private void log(Object o) { 
    if (DEBUG) { 
     Log.i(TAG, o.toString()); 
     FileLogger.appendToLog(o); 
    } 
} 

@Override 
public void onCreate() { 
    log("onCreate"); 
    handler = new Handler(); 
    handler.post(runnable); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    log("onStartCommand"); 

    return START_REDELIVER_INTENT; 
} 

@Override 
public void onDestroy() { 
    log("onDestroy"); 

} 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

} 

ServiceStopBroadcastReceiverTest:

/** 
* Simple broadcast receiver for catching a broadcast to stop the service. 
* 
* 
*/ 

public class ServiceStopBroadcastReceiverTest extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 

    Log.i("MyDebug_ServiceStopBroadcastReceiverTest", "onReceive ran, Calling stopService now."); 
    context.stopService(new Intent(context, ServiceTest.class)); 

} 

} 

ServiceSchedulerTest:

public class ServiceSchedulerTest { 

    private static ServiceSchedulerTest mInstance; 
    private AlarmManager alarmManager; 
    private Context ctx; 

    private static String TAG = "MyDebug_ServiceSchedulerTest"; 
    private static boolean DEBUG = true; 

    private static void log(Object o) { 
     if (DEBUG) { 
      Log.i(TAG, o.toString()); 
     } 
    } 

    // Singleton pattern 
    private ServiceSchedulerTest(Context ctx) { 
     this.ctx = ctx.getApplicationContext(); 
     alarmManager = (AlarmManager) this.ctx.getSystemService(Context.ALARM_SERVICE); 

    } 

    // Singleton pattern 
    public static ServiceSchedulerTest getInstance(Context ctx) { 
     if (mInstance == null) { 
      mInstance = new ServiceSchedulerTest(ctx); 
     } 
     return mInstance; 
    } 

    public void scheduleService(ScheduleUnit scheduleUnit) { 



     /** 
     * First part, scheduling START service 
     */ 

     // unique Id for Start 
     int uniqueIdForStart = (int) Calendar.getInstance().getTimeInMillis(); 

     log("Scheduling to START service at date: " + scheduleUnit.dateStart + " with request code: " + uniqueIdForStart); 

     // StartDate in millis 
     long millisToStartDate = scheduleUnit.dateStart.getTime(); 

     log("Scheduling start in millis: "+millisToStartDate); 

     // Start intent for service 
     Intent startIntent = new Intent(ctx, ServiceSchedulerTest.class); 

     // Pending intent, with uniqueId and intent parameter for alarmManager's set function 
     PendingIntent startPendingIntent = PendingIntent.getService(ctx, uniqueIdForStart, startIntent, 0); 

     // Setting the actual alarm with the start PendingIntent 
     alarmManager.set(AlarmManager.RTC_WAKEUP, millisToStartDate, startPendingIntent); 
     // ///////////////////////////////// 

     /** 
     * Second part, scheduling STOP service 
     */ 

     // unique Id for Start 
     int uniqueIdForEnd = (int) Calendar.getInstance().getTimeInMillis(); 
     //Increment to make sure it is different from uniqueIdForStart 
     uniqueIdForEnd++; 

     log("Scheduling to STOP service at date: " + scheduleUnit.dateEnd + " with request code: " + uniqueIdForEnd); 

     // EndDate in millis 
     long millisToEndDate = scheduleUnit.dateEnd.getTime(); 

     log("Scheduling end in millis: "+millisToEndDate); 

     // End intent for service -> calling a broadcast reciver to actually stop the service 
     Intent endIntent = new Intent(ctx, ServiceStopBroadcastReceiverTest.class); 

     // Penidng intent, with the uniqueId and the intent paramter for alarmManager'set function 
     PendingIntent endPendingIntent = PendingIntent.getBroadcast(ctx, uniqueIdForEnd, endIntent, 0); 

     // Setting alarm manager to call the Service stopping 
     alarmManager.set(AlarmManager.RTC_WAKEUP, millisToEndDate, endPendingIntent); 

    } 

} 

Протоколирование:

04-08 15: 13: 46,681 I/MyDebug_ServiceSchedulerTest (27581): Планирование запустить услугу по дате: ср Apr 08 15:15:00 CEST 2015 с запросом Код: -1725282887

04-08 15:13:46. 682: Я/MyDebug_ServiceSchedulerTest (27581): Планирование старт в Millis: 1428498900000

04-08 15: 13: 46.687: Я/MyDebug_ServiceSchedulerTest (27581): Планирование остановить работу на день: ср 8 апреля 15:16 : 00 CEST 2015 с запросом Код: -1725282881

04-08 15:13:46.687: I/MyDebug_ServiceSchedulerTest (27581): Планирование конец в Millis: 1428498960000

Проблема:

Служба фактически никогда не вызывается, он никогда не работает.

Помогите мне, если сможете.

E D I T:

Как CI_ нижеперечисленным я случайно добавил неправильный класс к моему методу GetService pendingIntent в. Изменение его на действительную службу он работает нормально

+0

'startPendingIntent' - это ожидающее намерения, созданное с помощью функции' PendingIntent.getService() ', но передано' Intent' а не «Сервис». –

+0

Нет, это прекрасно, из документов 'PendingIntent.getService:' ** «Параметры: намерение« Предназначение, описывающее услугу, которая должна быть запущена ». ** –

+1

Нет, это не нормально. Прочтите остальные документы: «Извлеките PendingIntent, который запустит службу, например вызов Context.startService()». Вы передаете «ServiceSchedulerTest», который не является «Сервисом». –

ответ

1

В ServiceSchedulerTest вы пытаетесь получить услугу PendingIntent для ServiceSchedulerTest, который, скорее всего, опечатка и должно быть ServiceTest

Intent startIntent = new Intent(ctx, ServiceSchedulerTest.class); 

должен Скорее всего, будет:

Intent startIntent = new Intent(ctx, ServiceTest.class);