Uruchom AlarmManager, jeśli urządzenie jest restartowane

W mojej aplikacji chcę uruchamiać kod codziennie o określonej godzinie używając AlarmManager. W dokumentacji Androida znalazłem to:

Zarejestrowane alarmy są zachowywane podczas uśpienia urządzenia [...] ale zostanie wyczyszczone, jeśli zostanie wyłączony i ponownie uruchomiony.

I w tym tkwi problem. Chcę uruchomić kod, nawet jeśli użytkownik ponownie uruchomi telefon. Jeśli użytkownik ponownie uruchomi telefon, musi ponownie uruchomić aplikację, aby ponownie uruchomić alarmy. Jak Mogę temu zapobiec? Na jest lepszy mechanizm, który powinienem użyć zamiast tego?
Author: stkent, 2013-07-16

3 answers

Utwórz Odbiornik rozruchowy używając następującego kodu:

public class BootBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context pContext, Intent intent) {
        // Do your work related to alarm manager
    }
}

W manifeście zarejestruj ten odbiornik:

<receiver
android:name="com.yourapp.BootBroadcastReceiver"
android:enabled="true" >
<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

I nie zapomnij dodać uprawnień w AndroidManifest.xml:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 33
Author: Android Expert,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-16 10:20:40

Use can u create service using broadcast reciever on device boot up

 <receiver android:enabled="true" android:name=".YourReceiver"
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

Pozwolenie:

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 7
Author: Srikanth Roopa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-16 10:16:51

Będziesz musiał dodać odbiornik rozruchowy w manifeście w ten sposób

<application ...  >

    <receiver android:name=".OnBootReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <uses-permission android:name="android.permission.WAKE_LOCK" />
        </intent-filter>
    </receiver>
</application>

A następnie utwórz klasę boot receiver w ten sposób...

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class OnBootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context ctxt, Intent intent) {
AlarmHelper.setAlarm(ctxt);
}
}

Moja klasa pomocnika alarm jest prosty początek dnia alarm jak ten...

public class AlarmHelper {

public static void testAlarm(Context context) {
Calendar when = Calendar.getInstance();
when.add(Calendar.SECOND, 10);
setAlarm(context, when);    
}

public static void setAlarm(Context context) {
Calendar when = Calendar.getInstance();
when.add(Calendar.DAY_OF_YEAR, 1);
when.set(Calendar.HOUR_OF_DAY, 0);
when.set(Calendar.MINUTE, 0);
when.set(Calendar.SECOND, 0);
setAlarm(context, when);
}

    @SuppressLint("SimpleDateFormat")
private static void setAlarm(Context context, Calendar when) {

SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());

Boolean showNotifications = prefs.getBoolean("PREF_SHOW_NOTIFICATIONS",
false);

if (showNotifications) {    
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);

am.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), AlarmManager.INTERVAL_DAY, getPendingIntent(context.getApplicationContext()));

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Log.i(TAG, "Alarm set " + sdf.format(when.getTime()));
}
}
 4
Author: CodeChimp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-16 10:20:42