How to implement WorkManager to run background process (isWifiEnabled) in your Android App?

This video shows steps to implement work manager API to run certain background tasks for your Android App. In this example it checks whether is Wifi is enabled or not and displays it in a toast message. However, other functionality can also be easily implemented in the doWork() method of the Worker class.

The steps for this App is referred from:
https://developer.android.com/topic/libraries/architecture/workmanager/basics

I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com

Complete source code and other details:

package com.programmerworld.workmanagerapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.work.OneTimeWorkRequest;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import androidx.work.WorkRequest;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

private TextView textView;
private WorkRequest uploadWorkRequest;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = findViewById(R.id.textView);

uploadWorkRequest = new OneTimeWorkRequest.Builder(UploadWorker.class)
.build();

// uploadWorkRequest = new PeriodicWorkRequest.Builder(UploadWorker.class,
// 15*60*1000, //15 mins is minimum
// TimeUnit.MILLISECONDS)
// .build();
}

public void buttonWorkManager(View view){
WorkManager
.getInstance(this)
.enqueue(uploadWorkRequest);

textView.setText("Work Manager executed!");
}
}

package com.programmerworld.workmanagerapp;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

public class UploadWorker extends Worker {
public UploadWorker(@NonNull Context context,
@NonNull WorkerParameters workerParams) {
super(context, workerParams);
}

@NonNull
@Override
public Result doWork() {

ContextCompat.getMainExecutor(getApplicationContext()).execute(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"WorkManager Started ...",
Toast.LENGTH_SHORT).show();

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(
Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()){
Toast.makeText(getApplicationContext(),
"Wifi Enabled",
Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(),
"Wifi Disabled",
Toast.LENGTH_LONG).show();
}
}
});
return null;
}
}

plugins {
id 'com.android.application'
}

android {
compileSdk 31

defaultConfig {
applicationId "com.programmerworld.workmanagerapp"
minSdk 31
targetSdk 31
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

def work_version = "2.7.1"
// (Java only)
implementation "androidx.work:work-runtime:$work_version"
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.programmerworld.workmanagerapp">

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.WorkManagerApp">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="96dp"
android:layout_marginTop="164dp"
android:onClick="buttonWorkManager"
android:text="Start Work Manager"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Leave a Reply

%d bloggers like this:
Available for Amazon Prime