Sample code for 30+ languages & platforms
Android™

Check for IMAP IDLE Mailbox Updates

See more IMAP Examples

Demonstrates the Chilkat Imap.IdleCheck method, which waits for mailbox updates after IdleStart has entered IDLE mode. The only argument is a timeout in milliseconds; 0 performs a non-blocking poll of notifications already received. This example waits up to 30 seconds.

Background: IdleCheck consumes the notifications already waiting on the connection rather than sending a new polling command. A typical monitoring loop repeatedly calls IdleCheck with a timeout and reacts when updates arrive. Because servers usually drop an idle connection after about 29 minutes, long-lived monitors periodically call IdleDone and then IdleStart again to refresh it.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    //  Demonstrates the Imap.IdleCheck method, which waits for IMAP IDLE mailbox updates and returns
    //  them.  The only argument is a timeout in milliseconds.  Use 0 for a non-blocking poll.

    CkImap imap = new CkImap();

    imap.put_Ssl(true);
    imap.put_Port(993);

    success = imap.Connect("imap.example.com");
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    success = imap.Login("user@example.com","myPassword");
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    success = imap.SelectMailbox("Inbox");
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    success = imap.IdleStart();
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    //  Wait up to 30 seconds for mailbox updates.  IdleCheck returns a string, so assign it to a
    //  string variable and check for success.
    int timeoutMs = 30000;
    String updates = imap.idleCheck(timeoutMs);
    if (imap.get_LastMethodSuccess() == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    Log.i(TAG, updates);

    success = imap.IdleDone();
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    success = imap.Disconnect();
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}