Solved

What is EventBus in Android

Posted on 2014-11-24
1
399 Views
Last Modified: 2014-11-30
Hi,
I have a following piece of Code :
package kujo.app.application;

import android.content.Context;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import de.greenrobot.event.EventBus;
import kujo.app.SoundManager;
import to.talk.logging.Logger;
import to.talk.logging.LoggerFactory;

public class Device {
    private static final String DEVICE_NOT_CREATED_EXCEPTION_MSG = "Create Device before use!";
    private static Context _context;
    private static Device _this;
    private static final Object _lock = new Object();
    private Logger _logger = LoggerFactory.getTrimmer(Device.class.getSimpleName());

    private Device(Context context) {
        _context = context;
    }

    public static Device create(Context context) {
        if (_this == null) {
            synchronized (_lock) {
                if (_this == null) {
                    _this = new Device(context);
                    EventBus.getDefault().register(_this);
                }
            }
        }
        return _this;
    }

    public void onEvent(SoundManager.SpeakerSettingChangedEvent event) {
        boolean isSpeakerEnabled = event.isSpeakerOn();
        setSpeakerPhoneOn(isSpeakerEnabled);
    }

    //not throwing exception to avoid exceptional handling
    public static Device getInstance() {
        return _this;
    }

    public void setSpeakerPhoneOn(boolean val) {
        AudioManager audioManager = getAudioManager();
        audioManager.setSpeakerphoneOn(val);
    }

    public void setMuteStatus(boolean val) {
        _logger.debug("set mute status: {}", val);
        AudioManager audioManager = getAudioManager();
        audioManager.setMicrophoneMute(val);
        _logger.debug("set mute status check (fetching): mic mute: {}, music active: {}",
                audioManager.isMicrophoneMute(), audioManager.isMusicActive());
    }

    public AudioManager getAudioManager() {
        return (AudioManager) _context.getSystemService(Context.AUDIO_SERVICE);
    }

    public boolean canPerformNumberVerificationFromCurrentDevice() {
        return hasCallingAbility();
    }

    public boolean hasCallingAbility() {
        TelephonyManager telephonyManager = (TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE);
        return telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE;

    }

    public String getNetworkType() {
        ConnectivityManager connectivityManager = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null) {
            String subtypeName = networkInfo.getSubtypeName();
            if (subtypeName != null && subtypeName.length() > 0) {
                return networkInfo.getTypeName() + "-" + subtypeName;
            } else {
                return networkInfo.getTypeName();
            }
        } else {
            return "no network";
        }

    }
}

Open in new window



I dont know what an EventBus exactly is. Please help me in finding out what does the following lines in above code do :
public static Device create(Context context) {
        if (_this == null) {
            synchronized (_lock) {
                if (_this == null) {
                    _this = new Device(context);
                    EventBus.getDefault().register(_this);
                }
            }
        }
        return _this;
    }

Thanks
0
Comment
Question by:Rohit Bajaj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 21

Accepted Solution

by:
Amitkumar Panchal earned 500 total points
ID: 40463673
EventBus is used to simplify the communication between components.

You can get the complete details about EventBus on its project site (https://github.com/greenrobot/EventBus).

Hope this helps.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question