如何在Android中始终运行服务

时间:2017-10-05 16:52:49

标签: android android-service

在我的应用程序中,我想使用服务来获取服务器请求 我应该永远运行这项服务而不是阻止它!

我在服务中写下面的代码,但只显示5次,接收到5步。然后不显示Toast

但我希望始终 getData() 并显示Toast 服务类:

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");
    }
}

我从互联网上复制这个服务代码,但只显示5次。 我希望show始终

如何编辑我的代码并修复它?请帮我。感谢

1 个答案:

答案 0 :(得分:1)

问题不在于服务,服务启动和继续生活只要应用程序还活着并且android没有杀死它。对于无限循环,替换&#34; for循环&#34; with&#34; while loop&#34;。以下循环不会结束。

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