0

I really need help! I have a server that sends me predicting time. I need to send request to the server every 10 minutes(even if the app closed) and ask the current time. if the time changed I need to make a notification to the client. the stop trigger is if the predicting time is now.

so - I have ClientActivity and I have the code to communicate the server.

How can I do that?

Thanks a lot!

public interface APIClient {

    @POST("/api/post_some_data")
    Call<PostResponse> getPostRequest(@Body PostRequest  body);

}

public class NetworkClient {

        public static final String BASE_URL = "http://*****:8080";
        public static Retrofit retrofit;
        /*
        This public static method will return Retrofit client
        anywhere in the appplication
        */
        public static Retrofit getRetrofitClient() {
            //If condition to ensure we don't create multiple retrofit instances in a single application
            if (retrofit == null) {
                //Defining the Retrofit using Builder
                retrofit = new Retrofit.Builder()
                        .baseUrl(BASE_URL) //This is the only mandatory call on Builder object.
                        .addConverterFactory(GsonConverterFactory.create()) // Convertor library used to convert response into POJO
                        .build();
            }
            return retrofit;
        }
    }
Mohamed AbdelraZek
  • 2,503
  • 4
  • 25
  • 36
eo1
  • 1
  • 2
  • If you want to have an exact repeating, you should use Alarm Manager to send request to server. – abbas oveissi May 28 '19 at 19:49
  • How can I do that ? Do you have any toturial? – eo1 May 28 '19 at 19:56
  • N.B. `NetworkClient` is all static. Why ? Don't do this. You are expecting that `Retrofit` is thread-safe. Maybe yes, maybe not. You won't notice any performance issue, if you instance a new NetworkClient when you need one. – PeterMmm May 28 '19 at 20:38
  • How to use AlarmManager: https://stackoverflow.com/questions/4459058/alarm-manager-example – PeterMmm May 28 '19 at 20:41
  • thanks a lot, i'll try it. alarm manager can work in the background? – eo1 May 30 '19 at 05:56

0 Answers0