Solved

What is EventBus in Android

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Samsung TAB S upgrade - just showing SAMSUNG 5 24
MySqlDump not dumping triggers 1 31
configure dependency in POM for new database 3 25
use lov values 2 50
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 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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

777 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