我正在尝试启动特定日期的服务。我的问题是应该启动服务的意图不会这样做。
这是我应该启动服务的助手
public class AlarmHelper {
public static void createAlarm(Context ctx, TRANSAZIONE_MENSILE tm) {
int giorniDifferenza = calcolaGiorni(tm.getMonthDay());
int secondiDifferenza = calcolaSecondi();
int minutiDifferenza = calcolaMinuti();
int oreDifferenza = calcolaOre();
Intent alarmIntent = new Intent(ctx, AlarmService.class);
alarmIntent.putExtra("secondi", secondiDifferenza);
alarmIntent.putExtra("minuti", minutiDifferenza);
alarmIntent.putExtra("ore", oreDifferenza);
alarmIntent.putExtra("giorni", giorniDifferenza);
alarmIntent.putExtra("requestCode", tm.getRequestCode());
ctx.startService(alarmIntent);
}
//other methods not relevant in the process
}
这是AlarmService
public class AlarmService extends Service {
Alarm alarm = new Alarm();
public void onCreate(){
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Bundle extras = intent.getExtras();
int sec = extras.getInt("secondi");
int min = extras.getInt("minuti");
int hour = extras.getInt("ore");
int days = extras.getInt("giorni");
int requestCode = extras.getInt("requestCode");
alarm.setAlarm(this, sec, min, hour, days, requestCode);
return START_STICKY;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public void onDestroy(){
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
}
最后这是BroadcastReceiver
public class Alarm extends BroadcastReceiver {
private static final int SECS = 1000;
private static final int MINS = 60000;
private static final int HOURS = 3600000;
private static final int DAYS = 86400000;
@Override
public void onReceive(Context ctx, Intent intent) {
PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
//qui metto il codice
//todo codice
//prendo il request code
int requestCode = intent.getIntExtra("requestCode", -1);
if (requestCode != -1) {
// cancello l'allarme che si sa mai
cancelAlarm(ctx, requestCode);
// ora bisogna anche che la transazione sia salvata
AlarmHelper.resolveAlarm(ctx, requestCode);
// ora ricreo l'allarme nuovo per il mese prossimo
AlarmHelper.restartAlarm(ctx, requestCode);
}
wl.release();
}
public void setAlarm(Context ctx, int sec, int min, int hours, int days, int requestCode) {
AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(ctx, Alarm.class);
i.putExtra("requestCode", requestCode);
PendingIntent pi = PendingIntent.getBroadcast(ctx, requestCode, i, 0);
int tempoInMilli = 0;
tempoInMilli += (SECS * sec);
tempoInMilli += (MINS * min);
tempoInMilli += (HOURS * hours);
tempoInMilli += (DAYS * days);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), tempoInMilli, pi);
}
public void cancelAlarm(Context context, int requestCode) {
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, requestCode, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
意图根本无法到达服务,我该如何解决?任何帮助将不胜感激