Я пытаюсь использовать Android Jetpack WorkManager, но код ничего не делает. Вот мои примеры кодов:
- SampleWorkManager.java
public class SampleWorkManager extends Worker {
private static final String TAG = "SampleWorkManager";
public SampleWorkManager(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
Data inputData=getInputData();
String name=inputData.getString("name");
Log.d(TAG, "doWork: name: "+name);
int number=inputData.getInt("number",0);
for(int i=0;i<number;i++){
Log.d(TAG, "doWork: our number is: "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return Result.success();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initWorkManager();
}
private void initWorkManager(){
Log.d(TAG, "initWorkManager: started");
Data data=new Data.Builder()
.putInt("number",10)
.putString("name","First Work")
.build();
Constraints constraints=new Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)
.setRequiresBatteryNotLow(true)
.setRequiresCharging(false)
.build();
OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(SampleWorkManager.class)
.setInputData(data)
.setConstraints(constraints)
.build();
WorkManager.getInstance().enqueue(oneTimeWorkRequest);
}
}
также, я использую следующую зависимость:
def work_version = "2.0.1"
implementation "androidx.work:work-runtime:$work_version"
Файл Logcat
11/02 22:32:39: Launching 'app' on Pixel 2 API 28(My AVD).
$ adb shell am start -n "com.example.sampleworkmanager/com.example.sampleworkmanager.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Waiting for process to come online...
Connected to process 5216 on device 'Pixel_2_API_28_My_AVD_ [emulator-5554]'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/mpleworkmanage: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
W/mpleworkmanage: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
D/MainActivity: initWorkManager: started
D/OpenGLRenderer: Skia GL Pipeline
D/: HostConnection::get() New Host Connection established 0xe1215e80, tid 5251
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0xe2471cc0: maj 3 min 1 rcv 4
D/EGL_emulation: eglMakeCurrent: 0xe2471cc0: ver 3 1 (tinfo 0xe9bc7740)
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
glUtilsParamSize: unknow param 0x000082da
I/mpleworkmanage: Background concurrent copying GC freed 10967(2MB) AllocSpace objects, 5(164KB) LOS objects, 49% free, 1647KB/3MB, paused 4.338ms total 231.484ms
D/EGL_emulation: eglMakeCurrent: 0xe2471cc0: ver 3 1 (tinfo 0xe9bc7740)
проблема в том, что яЯ не могу понять, что происходит, и в фоновом режиме не выполняется ни одна задача. пожалуйста, помогите мне.