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

如何将我的活动设置为监听服务?这是解决这个问题的最佳方法吗?

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

提前致谢

有帮助吗?

解决方案

我如何设置我的活动以聆听服务?这是解决此问题的最佳方法吗?

在我看来,你有三个主要选择:

  1. 轮询。这 Activity 定期询问 Service 获取最新数据。恕我直言,这个选项很糟糕,但它肯定是可能的。

  2. 回调。根据贾克斯的回答, Activity 注册一个回调对象(“观察者”) Service. 。这 Service 当数据更改时调用回调上的方法,从而更新 UI。您可以看到一个使用它的示例 Service 这里.

  3. 播送 Intents. 。这 Service 广播一个 Intent 通过 sendBroadcast() 关于数据更改。这 Activity 注册一个 BroadcastReceiver 使用 registerReceiver(), , 然后 BroadcastReceiver 收到传入广播的通知。这会触发 Activity 从加载最新数据 Service, ,或者可能只是为了从广播中的额外内容中获取最新数据 Intent. 。您可以看到使用该技术的示例 Service 这里.

其他提示

此声音等的良好候选Observer模式。基本上你的活动(观察员)将自身与后台服务(可观察)注册,您可以从您的活动推或拉数据。你在这种情况下,观察者将是你的活动和可观测将是您的服务。

如果你一无所知设计模式买入“Head First设计模式”,它是易于阅读和充满了巨大的信息。

PS:我在读它现在

我真的,真的不知道为什么没有人提到使用任何图书馆的EventBus一个简单的方法。这是当然的,如果你不使用RX。 我个人最喜欢的是EventBus通过GreenRobot。 https://github.com/greenrobot/EventBus

只需几行代码的,并且没有接口。触发一个事件,无论你想听听它。据解耦,它是线程安全的,它不会崩溃您的应用程序。

您需要使用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);
  }
}

您将有一个后台线程运行计算列表中的变化。这线程现在需要通知该名单已更新GUI的可能性。

您可以使用某种 ArrayAdapter 的获取数据到ListView中。一个ArrayAdapter有一个名为 adpater方法。 notifyDataSetChanged()每次调用此方法的适配器会看到相应的数据发生了变化,然后通知其应在下次更新的可能性列表视图。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top