标签: greenrobot-eventbus

使用像Otto或EventBus这样的事件库是一种处理活动,片段和后台线程之间关系的推荐方法

在大多数情况下,处理案件时

  • 用户thread(AsyncTask)执行后台处理
  • 将计算结果传回给ActivityFragment
  • Activity或者Fragment在用户线程完成后台处理之前可能会重新创建

到目前为止,从许多可靠的来源,我可以看到推荐的方法是使用 保留片段

来源

我不时地听说事件总线库可以处理活动,片段和后台线程之间的关系.(请参阅https://github.com/greenrobot/EventBus.它表示在活动,片段和后台线程表现良好)

我遇到了一些非常受欢迎的事件总线库

我想知道,当处理活动,碎片和后台线程之间的关系时,事件总线方法与保留碎片方法有何不同?

推荐哪种方式?

android event-bus android-asynctask otto greenrobot-eventbus

44
推荐指数
1
解决办法
2万
查看次数

EventBus - Subscriber类及其超类没有@subscribe注释的公共方法

我正在使用EventBus创建一个Android应用程序,用于将异步广播发布到其他类,但我在执行期间遇到了错误.

MainActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.gms.maps.model.LatLng;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class MainActivity extends AppCompatActivity {

    //Globals
    public String uname = null;
    public double lat = 0;
    public double lng = 0;

    //Get GUI handles
    public Button sendButton; //
    public EditText username;
    public Button MapButton; //
    public EditText LatBox;
    public EditText LngBox;


    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //register EventBus
        EventBus.getDefault().register(this); …
Run Code Online (Sandbox Code Playgroud)

android event-bus greenrobot-eventbus

27
推荐指数
7
解决办法
3万
查看次数

哪些Activity生命周期方法最适合注册/注销到事件总线?

在活动中注册注销事件总线(如otto,EventBus或tinybus)的最佳位置是什么?为什么?

  1. 的onCreate() - 的onDestroy()
  2. 在onStart() - 的onStop()
  3. 的onResume() - 的onPause()

Otto的例子使用onResume() - onPause(),EventBus提到onStart() - onStop(),我们需要在我们的应用程序中使用onCreate() - onDestroy()来更新活动的UI,即使它在后台也是如此.所以我想它可以是三个中的任何一个,取决于事件的性质和它们的处理,但我想知道是否还有其他任何应该考虑的事情.

android otto greenrobot-eventbus

23
推荐指数
2
解决办法
1万
查看次数

我可以使用greenrobot EventBus进行活动和服务之间的通信吗?

关于服务通信,我可以将EventBus库用作活动吗?

我在我的应用程序中尝试了以下内容:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  EventBus.getDefault().register(this);
  setContentView(R.layout.activity_music_player);
  Intent serviceIntent=new Intent(MusicPlayerActivityTest.this,MusicPlayerServiceTest.class);
  startService(serviceIntent);
  EventBus.getDefault().post(new SetSongList(songArraList, 0));
}

@Override
protected void onDestroy() {
  EventBus.getDefault().unregister(this);
  super.onDestroy();
}
Run Code Online (Sandbox Code Playgroud)

在我的服务中onEvent叫.

android android-service android-activity greenrobot-eventbus

18
推荐指数
2
解决办法
1万
查看次数

用Greenrobot Eventbus替换广播接收器以触发基于事件的功能和从服务到活动的数据传输是否有用?

我已经实现了一个服务,我处理状态更改(连接,断开连接,onServiceDiscoverd,onCharacteristicChange等)并通过gatt服务器从另一个设备接收数据.

我的问题是,使用Greenrobot Eventbus 在服务和活动之间替换广播接收器可以有效地处理事件吗?

android android-service bluetooth-lowenergy android-broadcast greenrobot-eventbus

17
推荐指数
1
解决办法
6164
查看次数

Greenrobot EventBus和Guava的EventBus之间的差异

我经常使用来自greenrobot的EventBus

https://github.com/greenrobot/EventBus

但我刚才意识到Guava有自己的EventBus

com.google.common.eventbus.EventBus

有人知道是否存在很大差异?

android event-bus greenrobot-eventbus

16
推荐指数
2
解决办法
5751
查看次数

片段中的GreenRobot EventBus错误:没有订阅者注册事件类

我有一个活动,它的布局包含一个FrameLayout.我使用framelayout作为片段容器.我使用FragmentManager事务替换FrameLayout中的片段.

在其中一个片段的onCreate方法中,我使用EventBus注册片段.

@Override
public void onCreate(){
  EventBus.getDefault().register(this);
  // other initialization code
}
Run Code Online (Sandbox Code Playgroud)

该片段在其布局中具有GridView.每当点击gridView中的项目时,我都会向EventBus发布一个事件

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
    View rootView = inflater.inflate(R.layout.fragment_category, container, false);
    gridView = (GridView) rootView.findViewById(R.id.categry_grid_view);
    gridAdapter = new CustomGridAdapter(getActivity());
    gridView.setAdapter(gridAdapter);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Category clickedCategory = gridAdapter.getItem(position);
            EventBus.getDefault().post(new MyEvent());
        }
    });
Run Code Online (Sandbox Code Playgroud)

此事件的事件处理程序方法位于同一片段中,即片段具有以下方法

public void onEvent(MyEvent e){
    //some code;
}
Run Code Online (Sandbox Code Playgroud)

这可以正常工作,直到应用程序失去焦点并变为非活动状态(由于按下主页按钮或屏幕锁定).当我再次激活应用程序时,不会调用事件的事件处理程序.我可以在LogCat中看到以下语句

com.example.app D/Event? No subscribers registered for event class com.example.app.MyEvent
com.example.app D/Event? No subscribers …
Run Code Online (Sandbox Code Playgroud)

android android-fragments greenrobot-eventbus

15
推荐指数
1
解决办法
2万
查看次数

EventBus,register和registerSticky方法

我使用greenrobot EventBus库在我的Android应用程序中的两个片段之间发送数据,我想知道register(Object b)方法和registerSticky(Object object)方法之间的差异是什么?

android event-bus greenrobot-eventbus

15
推荐指数
1
解决办法
1万
查看次数

跨多个进程的Otto/EventBus

是否可以post在一个进程中进行事件(例如,SyncAdapter其中包含android:process=":sync"manifest属性)并使用OttoEventBus在另一个进程中(在常规应用程序UI中)接收它?

我知道,IntentBroadcastReceiver跨多个进程通信工作得很好,但我想有简单性和灵活性奥托/ EventBus.

android android-syncadapter otto greenrobot-eventbus

12
推荐指数
1
解决办法
3439
查看次数

使用GreenRobot EventBus线程化事件

我刚刚开始关注GreenRobot的EventBus for Android,并对线程有疑问.

我有一个长期运行的进程,我想在后台线程上运行,当完成后,它会更新UI.

所以类似于:

public void onEventBackgroundThread(MyEvent event) {
        doSomeLongRunningProcess();
        updateUI();
    }
Run Code Online (Sandbox Code Playgroud)

显然updateUI()不能在这里调用因为它也会在后台运行.

那么推荐的处理方法是什么?从我的内部触发另一个onEventBackgroundThread()将在UI线程上运行的事件?或者从长时间运行的过程中解雇它?还是有更好的模式?

android greenrobot-eventbus

11
推荐指数
1
解决办法
9347
查看次数