Last active
July 13, 2019 17:14
-
-
Save Ciantic/c16e2ce82bb3803a83acbdae75d80d3a to your computer and use it in GitHub Desktop.
Listen for a home wifi (Android Q)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Android Q specific way to listen for home wifi | |
fun jobListenWifiHome(ctx: Context) { | |
val jobScheduler = getSystemService(ctx, JobScheduler::class.java) ?: return | |
if (jobScheduler.getPendingJob(6) != null) { | |
return | |
} | |
var netspec = WifiNetworkSpecifier.Builder() // This requires Android Q | |
.setSsid("your home wifi SSID") | |
.build() | |
var req = NetworkRequest.Builder() | |
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI) | |
.setNetworkSpecifier(netspec) | |
.build() | |
val name = ComponentName(ctx, UpdateHomeStateService::class.java) | |
val jobInfo = JobInfo.Builder(6, name) | |
// .setRequiresCharging(true) // Is home wifi *AND* charging | |
.setRequiredNetwork(req) | |
.build() | |
jobScheduler.schedule(jobInfo) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment