FileDocCategorySizeDatePackage
MailService.javaAPI DocAndroid 1.5 API10773Wed May 06 22:42:46 BST 2009com.android.email.service

MailService

public class MailService extends android.app.Service

Fields Summary
private static final String
ACTION_CHECK_MAIL
private static final String
ACTION_RESCHEDULE
private static final String
ACTION_CANCEL
private Listener
mListener
private int
mStartId
Constructors Summary
Methods Summary
public static voidactionCancel(android.content.Context context)

        Intent i = new Intent();
        i.setClass(context, MailService.class);
        i.setAction(MailService.ACTION_CANCEL);
        context.startService(i);
    
public static voidactionReschedule(android.content.Context context)


         
        Intent i = new Intent();
        i.setClass(context, MailService.class);
        i.setAction(MailService.ACTION_RESCHEDULE);
        context.startService(i);
    
private voidcancel()

        AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent();
        i.setClassName("com.android.email", "com.android.email.service.MailService");
        i.setAction(ACTION_CHECK_MAIL);
        PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
        alarmMgr.cancel(pi);
    
public android.os.IBinderonBind(android.content.Intent intent)

        return null;
    
public voidonDestroy()

        super.onDestroy();
        MessagingController.getInstance(getApplication()).removeListener(mListener);
    
public voidonStart(android.content.Intent intent, int startId)

        super.onStart(intent, startId);
        this.mStartId = startId;

        MessagingController controller = MessagingController.getInstance(getApplication());
        controller.addListener(mListener);
        if (ACTION_CHECK_MAIL.equals(intent.getAction())) {
            if (Config.LOGD && Email.DEBUG) {
                Log.d(Email.LOG_TAG, "*** MailService: checking mail");
            }
            // Only check mail for accounts that have enabled automatic checking.  There is still
            // a bug here in that we check every enabled account, on every refresh - irrespective
            // of that account's refresh frequency - but this fixes the worst case of checking 
            // accounts that should not have been checked at all.
            // Also note:  Due to the organization of this service, you must gather the accounts
            // and make a single call to controller.checkMail().
            ArrayList<Account> accountsToCheck = new ArrayList<Account>();
            for (Account account : Preferences.getPreferences(this).getAccounts()) {
                if (account.getAutomaticCheckIntervalMinutes() != -1) {
                    accountsToCheck.add(account);
                }
            }
            Account[] accounts = accountsToCheck.toArray(new Account[accountsToCheck.size()]);
            controller.checkMail(this, accounts, mListener);
        }
        else if (ACTION_CANCEL.equals(intent.getAction())) {
            if (Config.LOGD && Email.DEBUG) {
                Log.d(Email.LOG_TAG, "*** MailService: cancel");
            }
            cancel();
            stopSelf(startId);
        }
        else if (ACTION_RESCHEDULE.equals(intent.getAction())) {
            if (Config.LOGD && Email.DEBUG) {
                Log.d(Email.LOG_TAG, "*** MailService: reschedule");
            }
            reschedule();
            stopSelf(startId);
        }
    
private voidreschedule()

        AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent();
        i.setClassName("com.android.email", "com.android.email.service.MailService");
        i.setAction(ACTION_CHECK_MAIL);
        PendingIntent pi = PendingIntent.getService(this, 0, i, 0);

        int shortestInterval = -1;
        for (Account account : Preferences.getPreferences(this).getAccounts()) {
            if (account.getAutomaticCheckIntervalMinutes() != -1
                    && (account.getAutomaticCheckIntervalMinutes() < shortestInterval || shortestInterval == -1)) {
                shortestInterval = account.getAutomaticCheckIntervalMinutes();
            }
        }

        if (shortestInterval == -1) {
            alarmMgr.cancel(pi);
        }
        else {
            alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()
                    + (shortestInterval * (60 * 1000)), pi);
        }