【问题标题】:Android - Greenrobot EventBus in android?Android - Android 中的 Greenrobot EventBus?
【发布时间】:2016-10-22 11:25:06
【问题描述】:

我在我的项目中使用greenrobot:eventbus

compile 'org.greenrobot:eventbus:3.0.0'

问题

我正在使用来自EventBus 的 GPS,如下所示:

public class ConditionGPS {
    public static void statusCheck(Activity activity) {
        final LocationManager manager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            buildAlertMessageNoGps(activity);
        } else if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            EventGPS event = new EventGPS();
            event.setGPSMessage(true);
            EventBus.getDefault().post(event);
        }
    }

    private static void buildAlertMessageNoGps(final Activity activity) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setMessage(activity.getString(R.string.GPS))
                .setCancelable(false)
                .setPositiveButton(activity.getString(R.string.Yes), new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, final int id) {
                        activity.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                    }
                })
                .setNegativeButton(activity.getString(R.string.No), new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, final int id) {
                        dialog.cancel();
                        EventGPS event = new EventGPS();
                        event.setGPSMessage(false);
                        EventBus.getDefault().post(event);
                    }
                });
        final AlertDialog alert = builder.create();
        alert.show();
    }
}

解释:

在我的setNegativeButton 的警报对话框EventBus 中做得很好,但我在这部分有问题:

...
...
else if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                EventGPS event = new EventGPS();
                event.setGPSMessage(true);
                EventBus.getDefault().post(event);
            } 

我的activity 中收不到任何消息。这是我的activity

@Override
protected void onResume() {
    super.onResume();
    if (!EventBus.getDefault().isRegistered(this)) {
        EventBus.getDefault().register(this);
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (EventBus.getDefault().isRegistered(this)) {
        EventBus.getDefault().unregister(this);
    }
}

@Subscribe
public void onEvent(EventGPS eventGps) {
    boolean message = eventGps.getGPSMessage();
    if (!message) {
        txtMessage.setVisibility(View.VISIBLE);
        txtMessage.setText(getString(R.string.Should));
    }else if (message){
        //****HERE I CAN'T GET ANY MESSAGE****//
        initViews(key);
    }
}

这是我的EventGPS

public class EventGPS {
    private boolean GPSMessage;

    public boolean getGPSMessage() {
        return GPSMessage;
    }

    public void setGPSMessage(boolean GPSMessage) {
        this.GPSMessage = GPSMessage;
    }
}

【问题讨论】:

    标签: android android-event event-bus greenrobot-eventbus greenrobot-eventbus-3.0


    【解决方案1】:

    注册您的活动,但请确保您尚未注册,否则您将收到异常。

    @Override
    public void onStart() {
        super.onStart();
        if(!EventBus.getDefault().isRegistered(this))
        {  
            EventBus.getDefault().register(this);
        }
    }
    
    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
    

    【讨论】:

      【解决方案2】:

      您必须将您的活动注册为接收者。

      @Override
      public void onStart() {
          super.onStart();
          EventBus.getDefault().register(this);
      }
      
      @Override
      public void onStop() {
          super.onStop();
          EventBus.getDefault().unregister(this);
      }
      

      【讨论】:

      • @Denny Weinberg。我的问题不在这里。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多