› Foros › Multiplataforma › Desarrollo
package com.birik.habilitadordatos;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by miguel on 13/07/13.
*/
public class ServicioEncendidoApagadoDatos extends Service {
Timer timer = new Timer();
private int Chi=0;
Context context = this;
private long TIEMPO_PARA_ENCENDER = 0;
private long TIEMPO_ENCENDIDO = 0;
// hiloApagarEncerDatosImpl hilo ;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
startForeground(0, null);
try {
Bundle recivido = intent.getExtras();
double encen = Double.parseDouble( recivido.get("Encendido").toString());
double paraEncen = Double.parseDouble(recivido.get("ParaEncender").toString());
long Encendido = (new Double(encen * 1000)).longValue();
long ParaEncender = (new Double(paraEncen * 1000)).longValue();
TIEMPO_ENCENDIDO = Encendido;
TIEMPO_PARA_ENCENDER= ParaEncender;
}catch (Exception ex){
Log.w("Info", ex.getMessage());
}
if(TIEMPO_ENCENDIDO != 0 & TIEMPO_PARA_ENCENDER != 0){
crearTareaDeEncendidoDatos();
}else{
Toast.makeText( context,"Debes pasar unos valores diferentes de 0",Toast.LENGTH_LONG).show();
onDestroy();
}
return START_STICKY;
}
private void crearTareaDeEncendidoDatos(){
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
setMobileDataEnabled(getApplicationContext(),true);
Log.w("Info","Internet Activado");
}
},0 ,TIEMPO_PARA_ENCENDER + TIEMPO_ENCENDIDO);
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
setMobileDataEnabled(getApplicationContext(),false);
Log.w("Info","Internet Apagado");
}
},TIEMPO_ENCENDIDO , TIEMPO_PARA_ENCENDER + TIEMPO_ENCENDIDO);
}
@Override
public void onDestroy() {
setMobileDataEnabled(getApplicationContext(), true);
if (timer != null) {
timer.cancel();
}
super.onDestroy();
Toast.makeText(this, "Servicion Terminado", Toast.LENGTH_SHORT).show();
}
private void setMobileDataEnabled(Context context, boolean enabled) {
try {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.birik.habilitadordatos"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.birik.habilitadordatos.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- <service android:name=".ServicioEncendidoApagadoDatos" /> -->
<service
android:name="ServicioEncendidoApagadoDatos"
android:process=":my_process"
>
</service>
</application>
</manifest>