AlarmManager autostart
<p>I've these class, at start set an alarm and after 10 seconds, alarm starts. There is also a reading from sharedpreferences to check if user has enable or disable the alarm.
Now, I need stars alarm also when app is not active (like after reboot) or when app is killed.
AlarmReceiver:
MainActiAlarmRAlarmReceiver:eceiver:vity</p>

<pre><code>public class MainActivity extends Activity {
AlarmReceiver:AlarmReceiver:
    Boolean set;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this, AlarmReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
        intent, PendingIntent.FLAG_ONE_SHOT);

        // read from sharepreferences
//      [...]
        //

        if (set){

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);
        Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
        }

    }
</code></pre>

<p>AlarmReceiver:</p>

<pre><code>public final class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();

    }
}
</code></pre>

<p>Manifest:</p>

<pre><code>&lt;receiver android:name="AlarmReceiver"&gt;&lt;/receiver&gt;
</code></pre>
