Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use Alarm Manager. Because your timer can die. But Alarm will wake your app. Try this working code: It wakes CPU every 10 minutes and shows notification.</p> <p>Add to Manifest.xml:</p> <pre><code>... &lt;uses-permission android:name="android.permission.WAKE_LOCK"&gt;&lt;/uses-permission&gt; ... &lt;receiver android:process=":remote" android:name="Alarm"&gt;&lt;/receiver&gt; ... </code></pre> <p>Code:</p> <pre><code>public class Alarm extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG"); wl.acquire(); // Put here YOUR code. Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example wl.release(); } public void SetAlarm(Context context) { AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent i = new Intent(context, Alarm.class); PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); // Millisec * Second * Minute } public void CancelAlarm(Context context) { Intent intent = new Intent(context, Alarm.class); PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(sender); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload