Solved

What is EventBus in Android

Posted on 2014-11-24
1
380 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
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

733 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