1

I am working on a project that uses firebase job dispatcher to schedule a job that runs on intervals. But the thing is, I ran the code on the android studio emulator and it worked just fine. The job was scheduled and I received a toast from the service class showing that the execution of the code is successful, However I tried running it on a real time android phone, the code builds and runs on the device, but the job is never scheduled, no message is received. I tried everything and the jobService class wasn’t still called. I tried running the app again on the emulator and it works alright but it never worked on my real time device. Please what could be the cause for this. I added the firebase gradle dependency to the build.gradle file. I added the jobService to the manifest folder. I don’t just know what the cause for this predicament is. Please I need an explanation. THANKS.

MainActivity

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.jobdispatcher.Constraint;
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
import com.firebase.jobdispatcher.GooglePlayDriver;
import com.firebase.jobdispatcher.Job;
import com.firebase.jobdispatcher.Lifetime;
import com.firebase.jobdispatcher.RetryStrategy;
import com.firebase.jobdispatcher.Trigger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import static android.R.attr.start;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button start_job, stop_job;
    private static final String Job_Tag = "myJobTag";
    private FirebaseJobDispatcher jobDispatcher;

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

        jobDispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));

        start_job = (Button) findViewById(R.id.start_job);
        stop_job = (Button) findViewById(R.id.stop_job);

        start_job.setOnClickListener(this);
        stop_job.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.start_job){

            Job job = jobDispatcher.newJobBuilder()
                    .setService(MyFirebaseJobScheduler.class)
                    .setLifetime(Lifetime.FOREVER)
                    .setRecurring(true)
                    .setTag(Job_Tag)
                    .setTrigger(Trigger.executionWindow(1, 2))
                    .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
                    .setReplaceCurrent(false)
                    .setConstraints(Constraint.ON_ANY_NETWORK)
                    .build();
            jobDispatcher.mustSchedule(job);
            Toast.makeText(this, "Job scheduled.", Toast.LENGTH_SHORT).show();

        }else if(v.getId() == R.id.stop_job){
            jobDispatcher.cancel(Job_Tag);
            Toast.makeText(this, "Job canceled.", Toast.LENGTH_SHORT).show();
        }
    }
}

JobService Class

import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.firebase.jobdispatcher.JobParameters;
import com.firebase.jobdispatcher.JobService;

public class MyFirebaseJobScheduler extends JobService {

    BackgroundScheduledTask backgroundScheduledTask;

    @Override
    public boolean onStartJob(final JobParameters jobParameters) {
        Log.d("JobDispatcher", "Job Called");
        backgroundScheduledTask = new BackgroundScheduledTask(){
            @Override
            protected void onPostExecute(String s) {
                Log.d("JobDispatcher", "PostExecute");
                Toast.makeText(getApplicationContext(), "Message from back task = "+s, Toast.LENGTH_LONG).show();
                jobFinished(jobParameters, false);
            }
        };
        backgroundScheduledTask.execute();
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        return false;
    }

    public static class BackgroundScheduledTask extends AsyncTask<Void, Void, String>{

        @Override
        protected String doInBackground(Void... params) {
            Log.d("JobDispatcher", "In background");
            return "Hello from background Job";
        }
    }
}

Manifest

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

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

    <service android:name=".MyFirebaseJobScheduler"
        android:exported="false">
        <intent-filter>
            <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
        </intent-filter>
    </service>
</application>

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "26.0.2"

    defaultConfig {
        applicationId "com.cyclon.server"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.firebase:firebase-jobdispatcher:0.5.2'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
}
Double_M
  • 171
  • 2
  • 15

2 Answers2

1

You have set .setConstraints(Constraint.ON_ANY_NETWORK) that means It requires network connection. If you want to start service without Internet connection then you have to remove .setConstraints(Constraint.ON_ANY_NETWORK) while scheduling job.

immodi
  • 607
  • 1
  • 6
  • 21
  • Thanks a lot for your reply. But I initially ran the code without that constraint, and when it didn't work I added them to see if it would. – Double_M Apr 12 '18 at 12:09
  • And note, the code runs alright on the android studio emulator. On the emulator is schedules the job and the toast is shown, however when i try running it on my mobile phone it dose not call the job service class at all. I even tried using another phone and it does the same, I don't know if there are requirements that a phone would have to meet for firebase job dispatcher to schedule a Job. – Double_M Apr 12 '18 at 12:13
  • make sure your device have updated google play services and **Recurring** job scheduler will trigger within 60 seconds – immodi Apr 12 '18 at 12:33
  • Let me try that. Thanks – Double_M Apr 14 '18 at 14:04
  • I tried it, still didnt work. I downloaded google play service version 12.2.17. – Double_M Apr 17 '18 at 03:07
  • This may help you [http://blogs.quovantis.com/how-to-schedule-jobs-in-android-using-firebase-job-dispatcher/](http://blogs.quovantis.com/how-to-schedule-jobs-in-android-using-firebase-job-dispatcher/) – immodi Apr 17 '18 at 06:07
0

In case it will be useful to someone, in addition to all the other previous answers below,I report what I've experienced: for the same problem, with quite the same code structure, in my case it was solved by changing the version of firebase lib imported in build.gradle to a recent one. I had : implementation 'com.firebase:firebase-jobdispatcher:0.5.0' and I then I've put: implementation 'com.firebase:firebase-jobdispatcher:0.8.5' probably a more recent one will be good the same.

android_dev71
  • 517
  • 5
  • 7