Firebase Job Dispatcher only called once

时光怂恿深爱的人放手 提交于 2019-12-24 09:15:18

问题


I am new to this, I have implemented a demo referencing from official repo of firebase job dispatching API.

I want to run a specific task at every 2 minutes, and call Firebase and fetch a single value from there and update my TextView.

What happening is several times I waited 5 mins but nothing is called, I have debugged app and I found that onStartJob() is not getting called.

Many times it only called just once and then stops and never called again.

Here is the code:

DeviceSpaceService

public class DeviceSpaceService extends JobService {

    private static final String TAG = "DeviceSpaceService";

    private int val = 0;

    @Override
    public boolean onStartJob(final JobParameters job) {

        Log.e(TAG, "onStartJob() called with: job = [" + job + "]");

//        if (Build.VERSION.SDK_INT >= 18)
//            Log.e("DEVICE_SPACE",
//                    "Free space is " +
//                            (statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong() / 1024) + " Kb"
//            );


        new Thread(new Runnable() {
            @Override
            public void run() {
                complteJob(job);
            }
        }).start();
        return true;
    }

    private void complteJob(JobParameters job) {

        if (val == 10) {
            jobFinished(job, false);
            return;
        }
        Home.setAndSend(String.valueOf(val));
        val++;
        Log.e(TAG, "complteJob() called with: job = [" + val + "]");
    }

    @Override
    public boolean onStopJob(JobParameters job) {
        Log.e(TAG, "onStopJob() called with: job = [" + job + "]");
        return false;
    }
}

Home Activity

public class Home extends AppCompatActivity {

    static TextView tvMain;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        tvMain = (TextView) findViewById(R.id.tv_main);

        //Added dispatcher object as it is must to diapatch the job declared.
        FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));

        Job job = dispatcher.newJobBuilder().setLifetime(Lifetime.UNTIL_NEXT_BOOT)
                .setService(DeviceSpaceService.class).setTag("DeviceSpaceCheck").setRecurring(true).setReplaceCurrent(false)
                .setTrigger(Trigger.executionWindow(0,120)).build();

        dispatcher.mustSchedule(job);
    }

    public static void setAndSend(String val) {
        // Write a message to the database
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("message" + val);

        myRef.setValue("Hello, World!" + val);

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (tvMain != null)
                    tvMain.setText(dataSnapshot.getValue().toString());
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}

来源:https://stackoverflow.com/questions/43206040/firebase-job-dispatcher-only-called-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!