user8709108
user8709108

Reputation:

How to run service for always in Android

In my application I want use service for get request to server.
I should run this service for always and not stop it!

I write below code in service, but just show for 5 time and when receive to 5 step. then not show Toast!

But I want always getData() and show Toast.
Service class :

public class NotifyService extends Service {

    private static final String TAG = "HelloService";
    private boolean isRunning = false;

    @Override
    public void onCreate() {
        Log.i(TAG, "Service onCreate");
        isRunning = true;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.i(TAG, "Service onStartCommand");
        //Creating new thread for my service
        //Always write your long running tasks in a separate thread, to avoid ANR
        new Thread(new Runnable() {
            @Override
            public void run() {

                //Your logic that service will perform will be placed here
                //In this example we are just looping and waits for 5000 milliseconds in each loop.
                for (int i = 0; i < 5; i++) {
                    try {
                        Thread.sleep(5000);
                    } catch (Exception e) {
                    }
                    if (isRunning) {
                        ExploreSendData sendData = new ExploreSendData();
                        sendData.setPageIndex(1);
                        sendData.setPageSize(10);
                        sendData.setShowFollows(false);
                        sendData.setShowMovies(true);
                        sendData.setShowNews(true);
                        sendData.setShowReplies(false);
                        sendData.setShowSeries(true);
                        sendData.setShowSuggestions(false);

                        InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
                        Call<ExploreResponse> call = api.getExplore(new SharedPrefrencesHandler(NotifyService.this)
                                .getFromShared(SharedPrefrencesKeys.TOKEN.name()), sendData);

                        call.enqueue(new Callback<ExploreResponse>() {
                            @Override
                            public void onResponse(Call<ExploreResponse> call, Response<ExploreResponse> response) {
                                if (response.body().getData() != null && response.body().getStatusCode() != 401
                                        && response.body().getStatusCode() != 402) {

                                    Toast.makeText(NotifyService.this, "Test Show message ever 5second", Toast.LENGTH_SHORT).show();

                                }
                            }

                            @Override
                            public void onFailure(Call<ExploreResponse> call, Throwable t) {

                            }
                        });
                    }
                }
                //Stop service once it finishes its task
                stopSelf();
            }
        }).start();

        return Service.START_STICKY;
    }


    @Override
    public IBinder onBind(Intent arg0) {
        Log.i(TAG, "Service onBind");
        return null;
    }

    @Override
    public void onDestroy() {
        isRunning = false;
        Log.i(TAG, "Service onDestroy");
    }
}

I copy this service code from internet, but just show 5times. I want show always.

How can I edit my codes and fix it? Please help me. Thanks

Upvotes: 0

Views: 385

Answers (1)

Davi
Davi

Reputation: 1081

The problem is not in the service, services start and continue living as long as the app is alive and android doesn't kill it. For an infinite loop replace the "for loop" with "While loop". The below loop doesn't end.

while (true) {
  ......
  ......
  ......
}

Upvotes: 1

Related Questions