如何从后台服务更新Android活动中的信息

时间:2010-03-18 10:04:32

标签: java android android-listview observer-pattern

我正在尝试创建一个具有ActivityList信息的简单Android应用程序,当应用程序启动时,我计划启动一个不断计算数据的服务(它将会改变),我希望ActivityList成为与服务在应用程序生命周期内计算的数据同步。

如何设置我的Activity以收听服务?这是解决这个问题的最佳方法吗?

例如,如果您想象一个股票价格列表 - 数据将定期更改,并且需要与(在我的情况下)不断计算/获取数据的服务同步。

提前致谢

5 个答案:

答案 0 :(得分:94)

  

如何设置我的活动   听服务?这是   解决这个问题的最佳方法是什么?

我认为有三个主要选择:

  1. 轮询。 Activity会定期向Service询问最新数据。恕我直言,这个选项很糟糕,但肯定有可能。

  2. 回调。根据jax的回答,Activity使用Service注册一个回调对象(“观察者”)。 Service在数据更改时调用回调上的方法,从而更新UI。您可以看到使用Service here

  3. 的示例
  4. 广播IntentsService在数据更改时通过Intent广播sendBroadcast()Activity使用BroadcastReceiver注册registerReceiver(),并向BroadcastReceiver通知传入的广播。这会触发ActivityService加载最新数据,或者只是为了从广播Intent中获取最新数据。您可以通过Service here查看使用该技术的示例。

答案 1 :(得分:4)

这听起来像观察者模式的一个很好的候选人。基本上您的活动(The Observer)将使用后台服务(The Observable)注册自己,您可以从Activity中推送或提取数据。在这种情况下,您的观察者将是您的活动,而Observable将是您的服务。

如果您对“设计模式”一无所知,请购买“Head First Design Patterns”,它易于阅读并且充满了很多信息。

PS:我现在正在读它。

答案 2 :(得分:1)

我真的非常想知道为什么没有人提到使用任何库的EventBus的简单方法。这当然是你不使用RX。 我个人最喜欢的是GreenRobot的EventBus。 https://github.com/greenrobot/EventBus

只有几行代码,没有接口。发射一个事件,随时随地倾听。它是分离的,它是线程安全的,它不会崩溃您的应用程序。

答案 3 :(得分:0)

您将运行后台线程来计算列表中的更改。该线程现在需要通知GUI该列表已更新。

您可以使用某种ArrayAdapter将数据导入ListView。每次调用此方法时,ArrayAdapter都有一个名为adpater.notifyDataSetChanged()的方法,适配器将看到相应的数据已更改,然后通知列表视图它应该在下一种可能性时更新。

答案 4 :(得分:0)

您需要使用bindService()将活动与正在运行的服务绑定并与之通信。

public class BindingActivity extends Activity {
YourService mService;
boolean mBound = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

@Override
protected void onStart() {
    super.onStart();
    // Bind to Your Service
    Intent intent = new Intent(this, YourService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
    super.onStop();
    // Unbind from the service
    if (mBound) {
        unbindService(mConnection);
        mBound = false;
    }
}

/** Called when a button is clicked (the button in the layout file attaches to
  * this method with the android:onClick attribute) */
public void onButtonClick(View v) {
    if (mBound) {
        // Call a method from your Service.
        // However, if this call were something that might hang, then this request should
        // occur in a separate thread to avoid slowing down the activity performance.
        int num = mService.getRandomNumber();
        Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
    }
}

/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // We've bound to the running Service, cast the IBinder and get instance
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }
 };
}

和你的服务一样:

public class LocalService extends Service {
    // Binder given to clients
   private final IBinder mBinder = new LocalBinder();
   // Random number generator
   private final Random mGenerator = new Random();

/**
 * Class used for the client Binder.  Because we know this service always
 * runs in the same process as its clients, we don't need to deal with IPC.
 */
public class LocalBinder extends Binder {
    LocalService getService() {
        // Return this instance of LocalService so clients can call public methods
        return LocalService.this;
    }
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

/** method for clients */
public int getRandomNumber() {
  return mGenerator.nextInt(100);
  }
}