package nl.rmokveld.castnotifications;

import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.media.MediaRouter;

public class DiscoveryService extends BaseCastService {

    private static final String TAG = "DiscoveryService";
    private static final String ACTION_START = BuildConfig.APPLICATION_ID + ".action.START_DISCOVERY";
    private static final String ACTION_START_WAKEUP = BuildConfig.APPLICATION_ID + ".action.START_DISCOVERY_WAKEUP";
    private static final String ACTION_REMOVE_TIMEOUT = BuildConfig.APPLICATION_ID + ".action_REMOVE_TIMEOUT";
    private static final String ACTION_SET_TIMEOUT = BuildConfig.APPLICATION_ID + ".action_SET_TIMEOUT";

    public static void start(Context context, String tag) {
        context.startService(buildIntent(context, tag));
    }

    static Intent buildIntent(Context context, String tag) {
        return buildIntent(context, false, tag);
    }

    static Intent buildIntent(Context context, boolean wakeup, String tag) {
        return new Intent(context, DiscoveryService.class).setAction(wakeup ? ACTION_START_WAKEUP : ACTION_START)
                .putExtra("tag", tag);
    }

    public static void stop(Context context) {
        Log.d(TAG, "stop() called with: " + "context = [" + context + "]");
        context.stopService(new Intent(context, DiscoveryService.class));
    }

    public static void removeTimeout(Context context) {
        Log.d(TAG, "removeTimeout() called with: " + "context = [" + context + "]");
        context.startService(new Intent(context, DiscoveryService.class).setAction(ACTION_REMOVE_TIMEOUT));
    }

    public static void setTimeout(Context context) {
        Log.d(TAG, "setTimeout() called with: " + "context = [" + context + "]");
        context.startService(new Intent(context, DiscoveryService.class).setAction(ACTION_SET_TIMEOUT));
    }

    @Override
    public String getTAG() {
        return TAG;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(getTAG(), "onStartCommand() called with: " + "tag = [" + intent.getStringExtra("tag") + "], intent = [" + intent + "], flags = [" + flags + "], startId = [" + startId + "]");
        if (ACTION_START.equals(intent.getAction()) || ACTION_START_WAKEUP.equals(intent.getAction())) {
            if (!DeviceStateHelper.isWifiConnected(this)) {
                stopDiscovery();
                return START_NOT_STICKY;
            }
            if (ACTION_START_WAKEUP.equals(intent.getAction()))
                acquireWakeLocks();
            startDiscovery(false, DeviceStateHelper.isScreenTurnedOn(this) ? 10000 : 0);
        } else if (ACTION_REMOVE_TIMEOUT.equals(intent.getAction())) {
            resetTimeout(0);
        } else if (ACTION_SET_TIMEOUT.equals(intent.getAction())) {
            resetTimeout(10000);
        }
        return START_REDELIVER_INTENT;
    }

    @Override
    protected void onDiscoveryTimeout() {
        Log.d(getTAG(), "onDiscoveryTimeout() called with: " + "");
        mCastNotificationManager.getDiscoveryStrategy().onBackgroundDiscoveryStopped();
        stopSelf();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

@@ -33,16 +33,58 @@
 tions;%0A%0A
+import android.content.BroadcastReceiver;%0A
 import a
@@ -138,16 +138,53 @@
 Intent;%0A
+import android.content.IntentFilter;%0A
 import a
@@ -274,25 +274,37 @@
 rt.v
-7.media.MediaRout
+4.content.LocalBroadcastManag
 er;%0A
@@ -674,30 +674,27 @@
 ring ACTION_
-REMOVE
+SET
 _TIMEOUT = B
@@ -730,22 +730,19 @@
 .action_
-REMOVE
+SET
 _TIMEOUT
@@ -760,223 +760,227 @@
 ate 
-static final String ACTION_SET_TIMEOUT = BuildConfig.APPLICATION_ID + %22.action_SET_TIMEOUT%22;%0A%0A    public static void start(Context context, String tag) %7B%0A        context.startService(buildIntent(context, tag
+BroadcastReceiver mTimeoutBroadcastReceiver = new BroadcastReceiver() %7B%0A        @Override%0A        public void onReceive(Context context, Intent intent) %7B%0A            resetTimeout(intent.getLongExtra(%22timeout%22, 0
 ));%0A    
 %7D%0A%0A 
@@ -979,39 +979,49 @@
     
+  
-%7D%0A%0A    static Intent buildInten
+  %7D%0A    %7D;%0A%0A    public static void star
 t(Co
@@ -1057,23 +1057,37 @@
         
-return 
+context.startService(
 buildInt
@@ -1106,24 +1106,25 @@
  false, tag)
+)
 ;%0A    %7D%0A%0A   
@@ -1714,90 +1714,83 @@
     
-context.startService(new Intent(context, DiscoveryService.class).setAction
+LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent
 (ACTION_
 REMO
@@ -1777,38 +1777,35 @@
 w Intent(ACTION_
-REMOVE
+SET
 _TIMEOUT));%0A    
@@ -1789,32 +1789,55 @@
 ION_SET_TIMEOUT)
+.putExtra(%22timeout%22, 0)
 );%0A    %7D%0A%0A    pu
@@ -1977,173 +1977,400 @@
     
-context.startService(new Intent(context, DiscoveryService.class).setAction(ACTION_SET_TIMEOUT));%0A    %7D%0A%0A    @Override%0A    public String getTAG() %7B%0A        return TAG
+LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(ACTION_SET_TIMEOUT).putExtra(%22timeout%22, 10000));%0A    %7D%0A%0A    @Override%0A    public String getTAG() %7B%0A        return TAG;%0A    %7D%0A%0A    @Override%0A    public void onCreate() %7B%0A        super.onCreate();%0A        LocalBroadcastManager.getInstance(this).registerReceiver(mTimeoutBroadcastReceiver, new IntentFilter(ACTION_SET_TIMEOUT))
 ;%0A  
@@ -3094,209 +3094,8 @@
 0);%0A
-        %7D else if (ACTION_REMOVE_TIMEOUT.equals(intent.getAction())) %7B%0A            resetTimeout(0);%0A        %7D else if (ACTION_SET_TIMEOUT.equals(intent.getAction())) %7B%0A            resetTimeout(10000);%0A
     
@@ -3381,24 +3381,197 @@
 f();%0A    %7D%0A%0A
+    @Override%0A    public void onDestroy() %7B%0A        LocalBroadcastManager.getInstance(this).unregisterReceiver(mTimeoutBroadcastReceiver);%0A        super.onDestroy();%0A    %7D%0A%0A
     @Nullabl

Use LocalBroadcastManager to set timeout instead of unneccesary service startup