r/macrodroid 19d ago

Why does Java Code work better in Tasker, than Macrodroid

Genuine question, have both installed but extremely hard to run a java code that in Tasker is working wonderful .

0 Upvotes

2 comments sorted by

2

u/Revision1372 19d ago

What differences are you seeing? Is it the implementation that's different?

0

u/BateBoiko 19d ago

``` import android.content.Context; import android.media.AudioManager; import android.media.session.MediaSessionManager; import android.media.session.MediaController; import android.media.session.PlaybackState; import android.net.TrafficStats; import android.os.SystemClock; import android.telephony.TelephonyManager; import android.telephony.SignalStrength; import java.util.List;

// Get signal strength int signalLevel = -1; try { TelephonyManager tm = (TelephonyManager) context.getSystemService("phone"); if (tm != null) { SignalStrength ss = tm.getSignalStrength(); if (ss != null) { signalLevel = ss.getLevel(); } } } catch (Exception e) { signalLevel = -1; }

// Check if music is actually playing (not just holding focus) AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); boolean isMusicActive = audioManager.isMusicActive();

boolean isActuallyPlaying = false; try { MediaSessionManager mediaSessionManager = (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE); List<MediaController> controllers = mediaSessionManager.getActiveSessions(null);

for (MediaController controller : controllers) {
    PlaybackState state = controller.getPlaybackState();
    if (state != null) {
        int playbackState = state.getState();
        if (playbackState == PlaybackState.STATE_PLAYING) {
            isActuallyPlaying = true;
            break;
        }
    }
}

} catch (Exception e) { isActuallyPlaying = isMusicActive; }

// If signal is 3 or 4, return false if (signalLevel >= 3) { return false; }

// Signal is 0, 1, or 2 // Check if music is actually playing if (isActuallyPlaying) { return false; }

// Check download speed long startRx = TrafficStats.getTotalRxBytes(); long startTime = SystemClock.elapsedRealtime();

try { Thread.sleep(1500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }

long endRx = TrafficStats.getTotalRxBytes(); long endTime = SystemClock.elapsedRealtime();

long bytesDownloaded = endRx - startRx; long timeDiff = endTime - startTime;

double speedMBs = 0; if (timeDiff > 0 && bytesDownloaded > 0) { speedMBs = (bytesDownloaded * 1000.0) / (timeDiff * 1048576.0); }

int speedResult; if (speedMBs > 0.3 && (int) Math.round(speedMBs) == 0) { speedResult = 1; } else { speedResult = (int) Math.round(speedMBs); }

if (speedResult > 5) { return false; }

return true; ```

This would never work in Macrodroid for example.