Sto refactoring del codice in modo che la mia app recuperi i dati da un sito Web una volta al giorno in un dato momento. Dalla mia ricerca, sembra che AlarmManager
sia l'approccio più appropriato.AlarmManager, BroadcastReceiver e servizio non funzionanti
Il tutorial ho seguito è: http://mobile.tutsplus.com/tutorials/android/android-fundamentals-downloading-data-with-services/
Finora, AlarmManager
e il BroadcastReceiver
sembra funzionare, tuttavia la Service
non sembra iniziare (. Es onStartCommand
non sembra di essere chiamato)
Ecco i frammenti importanti del codice che ho finora:
MyActivity.java
private void setRecurringAlarm(Context context) {
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getDefault());
updateTime.set(Calendar.HOUR_OF_DAY, 20);
updateTime.set(Calendar.MINUTE, 30);
Intent downloader = new Intent(context, AlarmReceiver.class);
downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, downloader, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// should be AlarmManager.INTERVAL_DAY (but changed to 15min for testing)
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
Log.d("MyActivity", "Set alarmManager.setRepeating to: " + updateTime.getTime().toLocaleString());
}
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent dailyUpdater = new Intent(context, MyService.class);
context.startService(dailyUpdater);
Log.d("AlarmReceiver", "Called context.startService from AlarmReceiver.onReceive");
}
}
MyService.java
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService", "About to execute MyTask");
new MyTask().execute();
return Service.START_FLAG_REDELIVERY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private class MyTask extends AsyncTask<String, Void, boolean> {
@Override
protected boolean doInBackground(String... strings) {
Log.d("MyService - MyTask", "Calling doInBackground within MyTask");
return false;
}
}
}
AndroidManifest.xml
<application ...>
...
<service android:name="MyService"></service>
<receiver android:name="AlarmReceiver"></receiver>
</application>
Quando ho tr igger il setRecurringAlarm
nel MyActivity
il log viene stampato come previsto, allo stesso modo, ogni 15 minuti viene visualizzato il registro da AlarmReceiver
. Tuttavia, non vedo mai il registro da MyService
:(
Esempio di quello che vedo nei log:
DEBUG/MyActivity(688): Set alarmManager.setRepeating to: Jan 29, 2012 8:30:06 PM
DEBUG/AlarmReceiver(688): Called context.startService from AlarmReceiver.onReceive
DEBUG/AlarmReceiver(688): Called context.startService from AlarmReceiver.onReceive
Non riesco a capire cosa ho fatto di sbagliato - la mia comprensione dal Android Dev Docs è che in AlarmReceiver
quando chiamo context.startService(dailyUpdater)
che dovrebbe a sua volta chiamata onStartCommand
in MyService
, anche se questo non sembra essere il caso!
che cosa sto facendo di sbagliato che sta causando MyService
di non iniziare affatto?
+1 Grazie il codice aiuto me .. – Nirali
Sei anche in esecuzione in un comportamento Wakelock non ovvio di avvio dei servizi in risposta agli allarmi. Ci sono alcuni dettagli e raccomandazioni nelle risposte su http://stackoverflow.com/questions/25852309/does-alarmmanager-require-the-pendingintent-to-be-of-type-broadcastreceiver – ctate