【问题标题】:Update UI with background service使用后台服务更新 UI
【发布时间】:2015-06-11 12:18:14
【问题描述】:

我已经声明了一个服务类:

public class MyService extends Service{
 @Nullable
 @Override
 public IBinder onBind(Intent intent) {
     return null;
 }

 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
     new Parsing().execute();
     return Service.START_STICKY;
}

private class Parsing extends AsyncTask<Void, Void, Void> {
    List<MyObject> myList = null;
    MyAdapter adapter = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        myList = new ArrayList<MyObject>();
    }

    @Override
    protected Void doInBackground(Void... params) {
        while (downloadDataFromInternet)
            myList.add(new MyObject(par1, par2, par3, par4));
    }

    @Override
    protected void onPostExecute(Void result) {
        if (myList.size() > 0) 
            adapter = new MyAdapter(this, myList);
    }
}
}

现在我想每 10 分钟(例如)在活动处于后台时执行此服务,但我希望当活动回到前台时,MyFragment 的 listView 使用服务中声明的适配器。 你能帮我么?我不知道该怎么做。

【问题讨论】:

    标签: android android-activity android-service android-adapter


    【解决方案1】:

    如果您必须在 10 分钟间隔内准确执行服务。 您可以将线程调度程序用作:

    private final ScheduledExecutorService scheduler =
        Executors.newScheduledThreadPool(1);
    
    
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    
        //schedule a thread
        scheduler.schedule(new Runnable() {
            public void run() { 
                startservice();
            }
        }, 10, TimeUnit.MINUTES);
    
        return Service.START_STICKY;
    }
    
    private startService(){
        // you background service related work
    
        while (downloadDataFromInternet)
            myList.add(new MyObject(par1, par2, par3, par4));
    
        // schedule a new thread
        scheduler.schedule(new Runnable() {
            public void run() { 
                startservice();
            }
        }, 10, TimeUnit.MINUTES);
    }
    

    【讨论】:

    • 谢谢,但是关于更新 UI 吗?
    • 为了更新 UI,您可以在收到服务成功响应后从服务向 UI(活动/片段)发送广播。或者您可以使用处理程序,但需要检查片段/活动是否正在运行和可见。
    【解决方案2】:

    我的建议使用数据库:

    一旦此后台任务运行,然后将您的数据更新到数据库中。打开 UI 后,您就可以从数据库中读取值,也可以更新 UI。

    【讨论】:

    • 那么就没有办法使用适配器了吗?
    • 我想也许你可以通过其他方式使用它。但问题是如果应用程序崩溃或设备重新启动,所有这些案例适配器都会丢失。所以避免这些问题数据库更好。
    【解决方案3】:

    听起来您想在 Service 和 Activity 之间进行通信。虽然服务就像独立于特定的上下文,但活动不是。

    因此,Service 不能(不应该)直接与 Activity 对话,尤其是在 Activity 被杀死/重新创建的情况下。

    您应该做的是通过使用数据库或SharedPreferences 开发一种相互通信的方法,以便从服务中存储您的数据。然后,Activity 会获取这些数据并使用它。

    【讨论】:

      【解决方案4】:

      您可以使用 Timer Task 在特定时间段后执行某些任务

      1 你需要创建TimerTask的子类

      例如

      class Mytask extends Timertask{
       @Override
      public void run() {
      
          Handler handler = new Handler(Looper.getMainLooper());
          handler.post(new Runnable() {
                   //your code you want to run in UI thread
               });
           }
      }
      

      2 从您的服务启动此任务

                      Timer timer = new Timer();
                      MyTask task= new MyTask(getApplicationContext());
                      timer.scheduleAtFixedRate(task,1,1000*60*10); // for 10 min
      

      希望这是你想要的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-27
        • 1970-01-01
        • 1970-01-01
        • 2015-07-20
        • 2013-10-08
        • 1970-01-01
        • 1970-01-01
        • 2015-07-28
        相关资源
        最近更新 更多