-1

i am trying to make an app that switches the mobile data on and off , when the screen is on the mobile data should be on as well. so i was trying to stop the service when the user turns the screen on. i have a main activity that calls SwitchService class , is there any way to stop the service from inside the service itself ? i made a lot of googling but nothing worked ,, onDestroy() , stopSelf() ..... whats the problem ?

public class SwitchService extends Service {

Handler mHandler;

@Override
public void onCreate() {

}

private void switchOff() {


    scheduleNext(10*1000);
    setMobileDataEnabled(this,false);



}
private void switchOn() {


    scheduleNext2(10*1000);
    setMobileDataEnabled(this,true);
    System.out.println("still runnin");



}

private void scheduleNext(int time) {
    mHandler.postDelayed(new Runnable() {
        public void run() {

            switchOn();

        }
    },time);
}
private void scheduleNext2(int time) {
    mHandler.postDelayed(new Runnable() {
        public void run() {

            switchOff();
        }
    },time);
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    mHandler = new android.os.Handler();
    switchOn();

    new Thread(new Runnable(){

        @Override
        public void run() {
            checkLock();

        }

    }).start();


    return START_STICKY;
}


private void checkLock() {


    scheduleNextLockCheck(1*1000);

    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    boolean isScreenOn = powerManager.isScreenOn();


    if (isScreenOn) {

        System.out.println("Screen onnn");
        setMobileDataEnabled(this,true);
        SwitchService.this.onDestroy();
    }




}

private void scheduleNextLockCheck(int time) {
    mHandler.postDelayed(new Runnable() {
        public void run() {

            checkLock();

        }
    },time);
}

@Override
public void onDestroy() {
    super.onDestroy();


}

@Override
public IBinder onBind(Intent intent) {

    return null;
}

private void setMobileDataEnabled(Context context, boolean enabled) {
    final ConnectivityManager conman = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    Class conmanClass;
    try {
        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());
        @SuppressWarnings("unchecked")
        final Method setMobileDataEnabledMethod = iConnectivityManagerClass
                .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);

        setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
    } catch (Exception e) {
        e.printStackTrace();
    }

}
Abdullah Asendar
  • 574
  • 1
  • 5
  • 31
  • To stop a Service within the Service, use stopSelf(): http://developer.android.com/reference/android/app/Service.html#stopSelf() Good luck getting that code to run on Android 5 and above! http://stackoverflow.com/questions/26539445/the-setmobiledataenabled-method-is-no-longer-callable-as-of-android-l-and-later – Daniel Nugent Jul 20 '15 at 23:10
  • so i cant switch mobile data on/off on lollipop @DanielNugent – Abdullah Asendar Jul 21 '15 at 06:22
  • Yeah, it's not going to work unfortunately. If you search, there a ton of questions about that issue. Unless Google opens up a legit public API for this in the future, it's not possible anymore. – Daniel Nugent Jul 21 '15 at 06:38

1 Answers1

2

stopSelf will stop the service, but that doesn't stop handlers, threads, etc owned by the service. You have to do that by hand in the onDestroy method.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127