简体   繁体   English

Android-Android中的Greenrobot EventBus?

[英]Android - Greenrobot EventBus in android?

I am using from greenrobot:eventbus in my project: 我在项目中使用的是greenrobot:eventbus

compile 'org.greenrobot:eventbus:3.0.0'

Problem : 问题

I am using from EventBus for GPS like bellow : 我从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();
    }
}

Explain : 说明:

In my setNegativeButton of alert dialog EventBus good work, but I have problem in this section : 在我setNegativeButton的警告对话框EventBus良好的工作,但我在这部分问题:

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

I can't get any message in my activity .Here is my activity : 我的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);
    }
}

And here is my EventGPS : 这是我的EventGPS

public class EventGPS {
    private boolean GPSMessage;

    public boolean getGPSMessage() {
        return GPSMessage;
    }

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

Register your Activity, but make sure you are not already registered or you will get an exception. 注册您的活动,但请确保您尚未注册,否则您将获得例外。

@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);
}

You have to register your activity as receiver. 您必须将活动注册为接收者。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM