I'm trying to make a cron run every X time in the system android tag to call a service that I have made.
The features of cron that I want to do are the following:
Receiver
connection is activated so that when there is connection the service is executed and then deactivated this Receiver
(I have done see code below in ConnectivityReceiver ). Some of these characteristics I have already achieved by doing them separately, then I put you a code of what I have.
ConnectivityReceiver
public class ConnectivityReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
boolean noConnectivity =
intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if(!noConnectivity){
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
// only when connected or while connecting...
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
// if we have mobile or wifi connectivity...
if ((netInfo.getType() == ConnectivityManager.TYPE_MOBILE)
|| (netInfo.getType() == ConnectivityManager.TYPE_WIFI)) {
Intent i = new Intent(context, EnvioEstadisticasService.class);
startWakefulService(context, i);
// disable receiver after we started the service
disableReceiver(context);
}
}
}
}
}
/**
* Enables ConnectivityReceiver
*
* @param context
*/
public static void enableReceiver(Context context) {
ComponentName component = new ComponentName(context, ConnectivityReceiver.class);
context.getPackageManager().setComponentEnabledSetting(component,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
/**
* Disables ConnectivityReceiver
*
* @param context
*/
public static void disableReceiver(Context context) {
ComponentName component = new ComponentName(context, ConnectivityReceiver.class);
context.getPackageManager().setComponentEnabledSetting(component,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
}
SendServiceStandard
public class EnvioEstadisticasService extends IntentService {
private static EstadisticasDAO daoEst;
public EnvioEstadisticasService() {
super("EnvioEstadisticasService");
}
@Override
protected void onHandleIntent(Intent intent) {
//hago todas las operaciones en envio de estadisticas
// Release the wake lock provided by the WakefulBroadcastReceiver.
ConnectivityReceiver.completeWakefulIntent(intent);
}
}
OnBootReceiver
public class OnBootReceiver extends BroadcastReceiver {
private static final String TAG = OnBootReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Log.i(TAG, "EnvioEstadisService: entra en el on bootreceiver");
Intent i = new Intent(context, EnvioEstadisticasService.class);
startWakefulService(context, i);
}
}
}
And finally in manifest
<!-- Cron -->
<receiver android:name=".cron.OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name=".cron.ConnectivityReceiver"
android:enabled="false" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<service android:name=".services.EnvioEstadisticasService"
android:exported="false"
android:enabled="true">
</service>
<!-- -->
I have managed to activate the service when the device is turned on or restarting independently of the execution of the application, in addition to controlling the subject of the connection at the time of sending to be able to call another receiver.
Does anyone know how to make it run every X interval regardless of whether the application is running or not?