在大多数情况下,处理案件时
AsyncTask)执行后台处理Activity或FragmentActivity或者Fragment在用户线程完成后台处理之前可能会重新创建到目前为止,从许多可靠的来源,我可以看到推荐的方法是使用 保留片段
我不时地听说事件总线库可以处理活动,片段和后台线程之间的关系.(请参阅https://github.com/greenrobot/EventBus.它表示在活动,片段和后台线程中表现良好)
我遇到了一些非常受欢迎的事件总线库
我想知道,当处理活动,碎片和后台线程之间的关系时,事件总线方法与保留碎片方法有何不同?
推荐哪种方式?
android event-bus android-asynctask otto greenrobot-eventbus
我正在使用EventBus创建一个Android应用程序,用于将异步广播发布到其他类,但我在执行期间遇到了错误.
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) 在活动中注册和注销事件总线(如otto,EventBus或tinybus)的最佳位置是什么?为什么?
Otto的例子使用onResume() - onPause(),EventBus提到onStart() - onStop(),我们需要在我们的应用程序中使用onCreate() - onDestroy()来更新活动的UI,即使它在后台也是如此.所以我想它可以是三个中的任何一个,取决于事件的性质和它们的处理,但我想知道是否还有其他任何应该考虑的事情.
关于服务通信,我可以将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
我已经实现了一个服务,我处理状态更改(连接,断开连接,onServiceDiscoverd,onCharacteristicChange等)并通过gatt服务器从另一个设备接收数据.
我的问题是,使用Greenrobot Eventbus 在服务和活动之间替换广播接收器可以有效地处理事件吗?
android android-service bluetooth-lowenergy android-broadcast greenrobot-eventbus
我经常使用来自greenrobot的EventBus
https://github.com/greenrobot/EventBus
但我刚才意识到Guava有自己的EventBus
com.google.common.eventbus.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) 我使用greenrobot EventBus库在我的Android应用程序中的两个片段之间发送数据,我想知道register(Object b)方法和registerSticky(Object object)方法之间的差异是什么?
我刚刚开始关注GreenRobot的EventBus for Android,并对线程有疑问.
我有一个长期运行的进程,我想在后台线程上运行,当完成后,它会更新UI.
所以类似于:
public void onEventBackgroundThread(MyEvent event) {
doSomeLongRunningProcess();
updateUI();
}
Run Code Online (Sandbox Code Playgroud)
显然updateUI()不能在这里调用因为它也会在后台运行.
那么推荐的处理方法是什么?从我的内部触发另一个onEventBackgroundThread()将在UI线程上运行的事件?或者从长时间运行的过程中解雇它?还是有更好的模式?