2

i need to call a method after 2 minutes. i have tried handler and timer but when app close then it doesn't work. then i have call timer in service and start service on button click and stop service after 2 minutes. it works but problem is sometime service calls itself. below is my code.

MyService class

public class MyService extends Service {

private static Retrofit retrofit = null;

@Override
public IBinder onBind(Intent intent) {
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
    new CountDownTimer(120000, 1000) {
        public void onTick(long millisUntilFinished) {
            Log.d("message", "start");
        }
        public void onFinish() {
            Log.d("message", "finish");

            callMyMethod();
            stopService(new Intent(MyService.this, MainActivity.class));
        }
    }.start();
}

MainActivity class from where i start service.

stopService(new Intent(getContext(), MyService.class));
startService(new Intent(getContext(), MyService.class));

Manifest

<service android:name="MyService" android:enabled="true" android:exported="true"/>

NOTE: I WANT TO CALL SERVICE ONLY ON BUTTON CLICK AND STOP SERVICE AFTER 2 MINUTES. please need help to solve this issue or If there is other good solution other than services.

Updated: if network response in not success how can i handle to retry.

MyService class

public class MyService extends Service {

private static Retrofit retrofit = null;

@Override
public IBinder onBind(Intent intent) {
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
    new CountDownTimer(120000, 1000) {
        public void onTick(long millisUntilFinished) {
            Log.d("mess", "start");
        }
        public void onFinish() {
            Log.d("mess", "finish");

            selectDriverForJOb();
            stopSelf();//(new Intent(MyService.this, MainActivity.class));
        }
    }.start();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
     return START_NOT_STICKY;
}

private void selectDriverForJOb() {
    SharedPreferences sharedPreferences = getSharedPreferences("Login_credentials", MODE_PRIVATE);
    String user_id = sharedPreferences.getString("userID", null);
    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    APIServices_interface selectDriver_interface = retrofit.create(APIServices_interface.class);
    Call<SelectDriverforJobResult> call = null;
    try {
        call = selectDriver_interface.selectDriverforJob(user_id);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (call != null) {
        try {
            call.enqueue(new Callback<SelectDriverforJobResult>() {
                @Override
                public void onResponse(Call<SelectDriverforJobResult> call, Response<SelectDriverforJobResult> response) {
                    SelectDriverforJobResult selectDriver = response.body();
                    String message = selectDriver.getMessage();
                    if(!(message.equalsIgnoreCase("success"))){

                    }
                }

                @Override
                public void onFailure(Call<SelectDriverforJobResult> call, Throwable t) {
                    Log.d("mess : ", "Error Updating ");
                    //want to retry
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
}
Asif Ullah
  • 61
  • 14
  • use async task In async task use handler or countdown timer – Rohit Sharma Jun 27 '18 at 05:15
  • 1
    @Nilesh Rathod its not duplicate. i have checked that question already. that didn't solve my issue. – Asif Ullah Jun 27 '18 at 05:33
  • can you explain **service calls itself**? From where `startService` is called? – Vinod Maurya Jun 27 '18 at 06:24
  • 1
    @VinodMaurya calling service from main activity on button click. – Asif Ullah Jun 27 '18 at 06:58
  • 1
    What if user click the button for first time and service started and close the app ? Do you still need to perform task after 2 minutes or not ? – ADM Jun 27 '18 at 09:48
  • @ADM yes service will called after 2 minutes even if app is close or open. – Asif Ullah Jun 27 '18 at 10:21
  • 1
    Ok ! So Service is not the solution for this scenario as i have already mentioned in the answer below . This is clearly a use case of `AlarmManager` or `JobShedular`. Since you need Internet connection so its better to go with `JobShedular`. You can try `WorkManager` its a wrapper around `JobShedular` and works with all OS versions . – ADM Jun 27 '18 at 12:22

2 Answers2

3

override onStartCommand in service and return START_NOT_STICKY it will not restart the

public void onStartCommand()
{
     return START_NOT_STICKY
}

and use stopSelf() insted of stopService(new Intent(getContext(), MyService.class));

Zain Ul Abideen
  • 151
  • 1
  • 3
2

Your Service will not run after app close, Read background restriction. And this is not really a Service use case.

The solution can be to your problem is AlarmManager . Set an alarm for exact after 2 minutes and your Receiver willget called when its triggered . Before using AlarmManger give a read to setExactAndAllowWhileIdle() and setExact().

Alternatively you can also use WorkManager to set a Exact Job . But This will be an overkill if you do not need Job Constraints. So if your work does not depends of any Constraints like Network connection or other you can go with AlarmManger.

PS:- If your task depends on a Network connection then you should go with WorkManager. Read Schedule tasks with WorkManager .

ADM
  • 20,406
  • 11
  • 52
  • 83
  • 1
    yes i make a network request in my method.and i am retrieving a message in response. i want to stop service if response is "success" or again send request if response is not success. Please can you edit my code how to use WorkManager? – Asif Ullah Jun 27 '18 at 06:40
  • 1
    `how to use WorkManager?` can be a very broad answer . You should go through the link above and follow some tutorials [Sample](https://github.com/googlecodelabs/android-workmanager). Its not that hard to use and understand just go through the sample. – ADM Jun 27 '18 at 06:44