代码之家  ›  专栏  ›  技术社区  ›  user8709108

如何在Android中运行always服务

  •  0
  • user8709108  · 技术社区  · 8 年前

    服务 用于获取对服务器的请求。
    我应该永远运行这项服务,而不是停止它!

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

    但我想永远 getData() 并显示 干杯 .
    服务等级:

    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次。 我总是想要表演 .

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

    1 回复  |  直到 8 年前
        1
  •  1
  •   Davi    8 年前

    问题不在于服务,只要应用程序还活着,并且安卓没有杀死它,服务就会开始并继续存在。对于无限循环,将“For循环”替换为“While循环”。下面的循环并没有结束。

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