Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Take a look for AlarmManager class <a href="http://developer.android.com/reference/android/app/AlarmManager.html" rel="nofollow">http://developer.android.com/reference/android/app/AlarmManager.html</a> It's something like "cron"</p> <p>Ok, here is a piece of working code - first comes the activity class:</p> <pre><code>import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.os.PowerManager; import android.os.PowerManager.WakeLock; public class MainActivity extends Activity { private WakeLock wl; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //acquire wake lock PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "TAG"); wl.acquire(); // schedule alarm Intent i = new Intent(); i.setAction(WakeReciever.WAKE_INTENT); PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, i, 0); AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 120000, pIntent); } @Override protected void onPause() { wl.release(); super.onPause(); } } </code></pre> <p>Next comes BroadcastReceiver:</p> <pre><code>package com.test; import android.content.BroadcastReceiver; import android.content.Intent; import android.content.Context; import android.os.PowerManager; import android.os.PowerManager.WakeLock; public class WakeReciever extends BroadcastReceiver { public static final String WAKE_INTENT = "com.test.WAKE"; /** * @see android.content.BroadcastReceiver#onReceive(Context,Intent) */ @Override public void onReceive(Context context, Intent intent) { //acquire wake lock PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG"); wl.acquire(); //start activity Intent i = new Intent(); i.setClassName("com.test", "com.test.MainActivity"); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); wl.release(); } } </code></pre> <p>And finally don't forget about the manifest file:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;manifest android:versionCode="1" android:versionName="1.0" package="com.test" xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;uses-sdk android:minSdkVersion="8"/&gt; &lt;uses-permission android:name="android.permission.WAKE_LOCK"&gt;&lt;/uses-permission&gt; &lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt; &lt;activity android:label="@string/app_name" android:name=".MainActivity"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN"/&gt; &lt;category android:name="android.intent.category.LAUNCHER"/&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;receiver android:name=".WakeReciever" android:enabled="true"&gt; &lt;intent-filter&gt; &lt;action android:name="com.test.WAKE"&gt;&lt;/action&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;/application&gt; &lt;/manifest&gt; </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