如何使用FirebaseJobDispatcher可靠地替换CONNECTIVITY_CHANGE

时间:2017-05-21 10:28:57

标签: android android-jobscheduler firebase-job-dispatcher

我的应用程序定位到Android 7,最低限度为Android Android 4。

因此,听CONNECTIVITY_CHANGE(即使应用程序被杀)也不再有效。我想做的是

  1. 即使我的主应用程序被杀,当互联网连接从"不可用时变为" to" available",我想启动一个警报广播接收器。
  2. 我尝试使用以下代码实现

    MainActivity.java

    @Override
    public void onPause() {
        super.onPause();
        installJobService();
    }
    
    private void installJobService() {
        // Create a new dispatcher using the Google Play driver.
        FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
    
        Job myJob = dispatcher.newJobBuilder()
                // the JobService that will be called
                .setService(MyJobService.class)
                // uniquely identifies the job
                .setTag("my-unique-tag")
                // one-off job
                .setRecurring(true)
                // persist forever
                .setLifetime(Lifetime.FOREVER)
                // start between 0 and 60 seconds from now
                .setTrigger(Trigger.executionWindow(0, 60))
                // overwrite an existing job with the same tag
                .setReplaceCurrent(true)
                // retry with exponential backoff
                .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
                // constraints that need to be satisfied for the job to run
                .setConstraints(
                        // only run on any network
                        Constraint.ON_ANY_NETWORK
                )
                .build();
    
        dispatcher.mustSchedule(myJob);
    }
    

    然而,

    MyJobService.java

    import android.content.Context;
    
    import com.firebase.jobdispatcher.JobParameters;
    import com.firebase.jobdispatcher.JobService;
    
    import org.yccheok.jstock.gui.JStockApplication;
    
    /**
     * Created by yccheok on 21/5/2017.
     */
    
    public class MyJobService extends JobService {
        @Override
        public boolean onStartJob(JobParameters jobParameters) {
            Context context = this.getApplicationContext();
    
            android.util.Log.i("CHEOK", "Internet -> " + Utils.isInternetAvailable(context));
    
            // Answers the question: "Is there still work going on?"
            return false;
        }
    
        @Override
        public boolean onStopJob(JobParameters jobParameters) {
            // Answers the question: "Should this job be retried?"
            return true;
        }
    }
    

    但是,上述代码并不可靠。我如何测试

    1. 退出我的应用。
    2. 使用"强制停止"。
    3. ,通过设置明确杀死我的应用
    4. 关闭互联网。
    5. 开启互联网。
    6. 等几分钟。永远不会执行MyJobService
    7. 有没有可靠的方法,使用FirebaseJobDispatcher可靠地替换CONNECTIVITY_CHANGE?

      我经历过Firebase JobDispatcher - how does it work compared to previous APIs (JobScheduler and GcmTaskService)?,但我仍然无法找到让它可靠运行的方法。

1 个答案:

答案 0 :(得分:0)

不确定您是如何通过FirebaseJobDispatcher检测CONNECTIVITY_CHANGE的,但同样的情况我使用了广播

public class ConnectivityStateReceiver extends BroadcastReceiver {
String TAG = "MyApp";

@Override
public void onReceive(Context context, Intent intent) {

    Intent serviceIntent = new Intent(context, NetworkService.class);

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null) {
        return;
    } else if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) {
        Log.e(TAG, "Connected!");
        context.startService(serviceIntent);
    } else {
        Log.e(TAG, "Not Connected!");
        context.stopService(serviceIntent);
    }
}

}