使用Ottos总线进行通知轮询'

时间:2016-09-07 16:13:35

标签: android otto

在我的所有观看中,如果用户有通知,我会有一个带红点的图标,所以我创建了一个不断(每隔几分钟)轮询的课程,看看他们是否有通知。除了@Subscrtibe永远不会被触发。我认为可能原因之一就是我在HomeActivity注册之前开始发帖但是我没有认为那会是一个问题吗?

public class NotificationUtils {

    private static Timer timer;

    private static final int MINUTES = 1000 * 60;
    private static Context applicationContext;

    public static void startTask(Context context){
        checkNotifications();
        applicationContext = context;
        timer = new Timer();
        timer.schedule(task, 0L, MINUTES);
    }

    public static void killTask(){
        timer.cancel();
        timer.purge();
    }

    static TimerTask task = new TimerTask() {
        @Override
        public void run() {
            checkNotifications();
        }
    };

    private static void checkNotifications(){
        RestInterface service = RestService.getRequestInstance(applicationContext);
        Call<Boolean> call = service.hasNotificatrions(SessionUser.getInstance().getUser().getId());
        call.enqueue(new Callback<Boolean>() {
            @Override
            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                sendNotificationImage(response.body());
            }

            @Override
            public void onFailure(Call<Boolean> call, Throwable t) {
                Log.w("TimerTask", "Failed");
            }
        });
    }

    private static void sendNotificationImage(boolean hasUnreadNotifications){
        if(hasUnreadNotifications) {
            BusProvider.getInstance().post(R.drawable.notification_alert_icon); 
        } else { 
            BusProvider.getInstance().post(R.drawable.notification_icon);
        }
    }
}

当用户成功登录时,我开始轮询......

public class LoginActivity extends AppCompatActivity {
    ....
    successfulLogin(){
        ....
        NotificationUtils.startTask(getApplicationContext());
    }
}

然后我的HomeActivity我注册了这个课程并订阅..

public class HomeActivity extends AppCompatActivity {
    ....
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_home);
         BusProvider.getInstance().register(this);
    }
    ....
    @Subscribe
    public void setNotification(int imageResource){
         notificationButton.setImageResource(imageResource); 
         Log.w("HomeActivity", "Setting resource"); 
    }
}

编辑..添加BusProvider类

public final class BusProvider {
    private static final Bus bus = new Bus();

    public static Bus getInstance(){
        return bus;
    }

    private BusProvider(){}
}

1 个答案:

答案 0 :(得分:2)

您不应将Java primitive types用作事件类。您可以使用:

public class DisplayNotification {
    private int resourceId

    public DisplayNotification(int resourceId){
        this.resourceId = resourceId;
    }

    public int getResourceId(){
        return this.resourceId;
    }
}

将您的功能签名更改为

@Subscribe
public void setNotification(DisplayNotification event){...}

并将其发布如下:

BusProvider.getInstance().post(new DisplayNotification (R.drawable.notification_icon));