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;
//www.java2s.comimport 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
*/publicclass ServiceRebooter extends BroadcastReceiver {
publicstaticfinal String TAG = ServiceRebooter.class.getCanonicalName();
@Override
publicvoid onReceive(Context context, Intent intent) {
checkService(context);
}
publicstaticboolean checkService(Context context) {
// If we haven't yet initialized our modem, do so
if (!isServiceRunning(context)) {
startService(context);
return false;
} else {
return true;
}
}
privatestaticvoid 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);
}
privatestaticboolean 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
*/privatestaticvoid 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);
}
}