GCM Network Manager does not perform tasks

Asked

Viewed 34 times

1

Hello! Gentlemen I have two tasks and I notice that the same ones are not being executed, I called them in the main activity in onCreate() however if I go to another activity the tasks are not called, qdo I go back and I wait the determined time there is the execution, another detail is that of the two tasks one performs only once One task makes a GET and receives a json is a quick task while the other one gets data from the database and makes a POST, I’m using the lib loopj for the network.

    gnm = GcmNetworkManager.getInstance(this);

    PeriodicTask task1 = new PeriodicTask.Builder()
            .setService(AtualizacaoService.class)
            .setPeriod(50)
            .setFlex(10)
            .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED)
            .setRequiresCharging(false)
            .setUpdateCurrent(true)
            .setTag("atualizacao")
            .build();

    PeriodicTask task2 = new PeriodicTask.Builder()
            .setService(EnviarService.class)
            .setPeriod(80)
            .setFlex(10)
            .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED)
            .setRequiresCharging(false)
            .setUpdateCurrent(true)
            .setTag("envio")
            .build();

    gnm.schedule(task1);
    gnm.schedule(task2);

One of the services

public class AtualizacaoService extends GcmTaskService {
   private static final String TAG = "AtualizacaoService";

   private CustomSharedPreference shared ;
   private OrdemServicoService service ;
   private SincronismoService sincronismoService;
   private Sincronizacao sinc;
   private Date ultimaAtualizacao;
   private Map<String, String> params = new HashMap<String,String>();
   private SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy_HH:mm:ss", Locale.getDefault());

   @Override
   public void onInitializeTasks() {
       super.onInitializeTasks();
       Log.i(TAG, "onInitializeTasks()");
   }

   @Override
   public int onRunTask(TaskParams taskParams) {
       Log.i(TAG, "onRunTask()");
       retrieverUpdateFromRemoteServer();

       return GcmNetworkManager.RESULT_SUCCESS;
   }

   public void retrieverUpdateFromRemoteServer(){

    service = new OrdemServicoService(getApplicationContext());
    sincronismoService = new SincronismoService(getApplicationContext());
    User user = ((Aplicacao) getApplicationContext()).getLoginUser();

    params.put("data", sdf.format(this.ultimaAtualizacao)  );
    params.put(Helper.TOKEN, user.getToken());
    params.put(Helper.SERVIDOR, "1");

    CustomRequest serverRequest = new CustomRequest(
            Helper.PATH_TO_SERVER_ATUALIZACAO ,
            this.params,
            createRequestSuccessListener(),
            createRequestErrorListener());

    serverRequest.setRetryPolicy(new DefaultRetryPolicy(
            Helper.MY_SOCKET_TIMEOUT_MS * 2,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(serverRequest);
}
  • Which version of android you are using. GCM has some problems with android versions. I recommend switching to android-job Vernote, I never had a problem.

  • Hello my dear, good afternoon, look I had had a previous problem and so I did a "Ambi" after I realized that it was the cause of the second task not actually running she was yes one more time, removed and went all right, i know my Smart is bronca Android is the 4.4 more now ta of good worth.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.