title: How to show toast message after a period of time in android?
body: <p>I am showing toast message after every 20 seconds from current time but if I going out the app it is not working. Here is my code: </p>
  
  <pre><code>public class Main extends Activity {
      final static private long ONE_SECOND = 1000;
      final static private long TWENTY_SECONDS = ONE_SECOND * 20;
  
      PendingIntent pi;
      BroadcastReceiver br;
      AlarmManager am;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          br = new BroadcastReceiver() {
              @Override
              public void onReceive(Context c, Intent i) {
                  Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show();
                  Log.i("Receive message in every five seconds", "message");
              }
          };
          registerReceiver(br, new IntentFilter("com.authorwjf.wakeywakey"));
          pi = PendingIntent.getBroadcast(this, 0, new Intent(
                  "com.authorwjf.wakeywakey"), 0);
          am = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
  
          am.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(),
                  TWENTY_SECONDS, pi);
      }
  
      @Override
      protected void onDestroy() {
          am.cancel(pi);
          unregisterReceiver(br);
          super.onDestroy();
      }
  }
  </code></pre>
  
  <p>My question is if the app is not running but still it can show toast message? How can it possible in android?</p>