简体   繁体   中英

How to fix [Found: 'android.content.Context', required: 'androidx.lifecycle.LifecycleOwner' ] in BroadcastReceiver?

This is not duplicate since my problem is Wrong 1st argument type. Found: 'android.content.Context', required: 'androidx.lifecycle.LifecycleOwner' Wrong 1st argument type. Found: 'android.content.Context', required: 'androidx.lifecycle.LifecycleOwner' and i didn't got any answer to fix this issue. I'm trying to implement work manager in backround service.

I've tried to Cast as AppCompatActivity, LifeCycleOwner, ... context it self won't work also...

Here's The code:

Constraints constraints = new Constraints.Builder()
        .setRequiresCharging(true)
        .build();

final Data data = new Data.Builder()
        .putDouble(USER_LAT, maps.get(i).getUser_lat())
        .putDouble(USER_LNG, maps.get(i).getUser_lng())
        .putLong(MAP_ID, id)
        .putString(METHOD, maps.get(i).getMethod())
        .putString(STATUS, maps.get(i).getStatus())
        .putString(RECIEVED_DATA, maps.get(i).getCreate_time())
        .build();

final OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(DatabaseToApiWorker.class)
        .setInputData(data)
        .setConstraints(constraints)
        .setInitialDelay(delay, TimeUnit.SECONDS)
        .build();

WorkManager.getInstance().enqueue(oneTimeWorkRequest);
WorkManager.getInstance().getWorkInfoByIdLiveData(oneTimeWorkRequest.getId())
        .observe((LifecycleOwner) context, new Observer<WorkInfo>() {
            @Override
            public void onChanged(WorkInfo workInfo) {
                if (workInfo != null)
                    Log.e("ServiceWorker",workInfo.getState().name());
                if (workInfo.getState().isFinished())
                {
                    if (workInfo.getState() == WorkInfo.State.SUCCEEDED){

                        Data data1 = workInfo.getOutputData();
                        Long id = data1.getLong(DatabaseToApiWorker.MAP_ID_WORKER,0);
                        appDatabase.getMapDAO().deleteIt(id);
                        Log.e("ServiceWorker", String.valueOf(id));
                    } else if (workInfo.getState() == WorkInfo.State.FAILED) {

                        Log.e("ServiceWorker", String.valueOf(id)+" Sending Failed");
                    }

                } else {

                    Log.e("WORKER", String.valueOf(id)+" Failed");
                }
            }
        });

So the solution i found my problem was implementing a custom lifecyclerOwner, here's the link .

Here's The code: Add implementation:

public class MyLocationService extends BroadcastReceiver implements LifecycleOwner {
private LifecycleRegistry lifecycleRegistry;

In OnReceive ADD:

    lifecycleRegistry = new LifecycleRegistry(this);
    lifecycleRegistry.markState(Lifecycle.State.CREATED);

Add this below to your class:

@NonNull
@Override
public Lifecycle getLifecycle() {
    return lifecycleRegistry;
}

then change context to this::getLifecycle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM