Android - android 中的 Greenrobot EventBus?
Android - Greenrobot EventBus in android?
我在我的项目中使用 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;
}
}
您必须将您的 activity 注册为收件人。
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
注册您的 Activity,但请确保您尚未注册,否则您将遇到异常。
@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);
}
我在我的项目中使用 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;
}
}
您必须将您的 activity 注册为收件人。
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
注册您的 Activity,但请确保您尚未注册,否则您将遇到异常。
@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);
}