Android Open Source - sms-smap-gateway Service Rebooter






From Project

Back to project page sms-smap-gateway.

License

The source code is released under:

GNU General Public License

If you think the Android project sms-smap-gateway listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.android.smap.sms;
//w w w .j  a  v  a 2 s.  c o  m
import java.util.Calendar;

import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * BootChecker just makes sure our service is running after the phone is booted
 */
public class ServiceRebooter extends BroadcastReceiver {

  public static final String  TAG  = ServiceRebooter.class.getCanonicalName();

  @Override
  public void onReceive(Context context, Intent intent) {
    checkService(context);
  }

  public static boolean checkService(Context context) {
    // If we haven't yet initialized our modem, do so
    if (!isServiceRunning(context)) {
      startService(context);
      return false;
    } else {
      return true;
    }
  }

  private static void startService(Context context) {
    Log.d(TAG, "=================");
    Log.d(TAG, "STARTING SERVICES");
    Log.d(TAG, "=================");

    Intent serviceIntent = new Intent(context, GatewayService.class);
    context.startService(serviceIntent);
    scheduleRebootService(context);
  }

  private static boolean isServiceRunning(Context context) {
    ActivityManager manager = (ActivityManager) context
        .getSystemService(Context.ACTIVITY_SERVICE);

    for (RunningServiceInfo service : manager
        .getRunningServices(Integer.MAX_VALUE)) {
      if ("com.android.smap.services.RelayService"
          .equals(service.service.getClassName())) {
        return true;
      }
    }
    return false;
  }

  /**
   * Trigger a reboot tomorrow at midnight
   */
  private static void scheduleRebootService(Context context) {
    Intent serviceIntent = new Intent(context, ServiceRebooter.class);
    PendingIntent sender = PendingIntent.getService(context, 0,
        serviceIntent, 0);

    // reboot each day to keep things happy
    AlarmManager am = (AlarmManager) context
        .getSystemService(Context.ALARM_SERVICE);

    // advance to tomorrow
    Calendar now = Calendar.getInstance();
    now.add(Calendar.HOUR, 24);

    // floor our date
    int day = now.get(Calendar.DAY_OF_MONTH);
    int mo = now.get(Calendar.MONTH);
    int yr = now.get(Calendar.YEAR);
    now.set(yr, mo, day, 0, 0);

    // set the alarm
    am.set(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), sender);
  }
}




Java Source Code List

com.android.smap.AppConfig.java
com.android.smap.GatewayApp.java
com.android.smap.PhoneStateWrapper.java
com.android.smap.activities.BaseActivity.java
com.android.smap.activities.FragmentWrapperActivity.java
com.android.smap.activities.MainActivity.java
com.android.smap.adapters.MenuDrawerAdapter.java
com.android.smap.api.ApiConstants.java
com.android.smap.api.models.Gojo.java
com.android.smap.api.models.Node.java
com.android.smap.api.models.SmapException.java
com.android.smap.api.models.Status.java
com.android.smap.api.models.validator.ModelValidator.java
com.android.smap.api.models.validator.Required.java
com.android.smap.api.requests.ApiRequest.java
com.android.smap.api.requests.GojoRequest.java
com.android.smap.api.requests.StatusRequest.java
com.android.smap.commonsware.wakefull.AlarmReceiver.java
com.android.smap.commonsware.wakefull.WakefulIntentService.java
com.android.smap.controllers.ControllerErrorListener.java
com.android.smap.controllers.ControllerError.java
com.android.smap.controllers.ControllerFactory.java
com.android.smap.controllers.ControllerListener.java
com.android.smap.controllers.Controller.java
com.android.smap.controllers.EmailController.java
com.android.smap.controllers.GojoController.java
com.android.smap.controllers.UIPagingController.java
com.android.smap.controllers.UIRequestController.java
com.android.smap.fragments.BaseFragment.java
com.android.smap.fragments.HomeFragment.java
com.android.smap.models.MenuDrawerItem.java
com.android.smap.models.TextMessage.java
com.android.smap.samuel.Samuel.java
com.android.smap.sms.CellularModem.java
com.android.smap.sms.GatewayService.java
com.android.smap.sms.MonitorService.java
com.android.smap.sms.ScheduleMonitoringListener.java
com.android.smap.sms.ServiceRebooter.java
com.android.smap.ui.ArrayVelocAdapter.java
com.android.smap.ui.OnErrorTouchedListener.java
com.android.smap.ui.Operation.java
com.android.smap.ui.VelocAdapter.java
com.android.smap.ui.ViewBinder.java
com.android.smap.ui.ViewQuery.java
com.android.smap.ui.views.AnimatedNetworkImageView.java
com.android.smap.ui.views.ErrorView.java
com.android.smap.ui.views.FontableTextView.java
com.android.smap.utils.BitmapLruCache.java
com.android.smap.utils.MWAPNUtils.java
com.android.smap.utils.MWAnimUtil.java
com.android.smap.utils.MWAssetsUtils.java
com.android.smap.utils.MWConnectionUtils.java
com.android.smap.utils.MWEmailUtils.java
com.android.smap.utils.MWUiUtils.java
com.android.smap.utils.PreferenceWrapper.java
com.android.smap.utils.Refresher.java
com.android.smap.utils.UriBuilder.java