How do I combine this two application class for Android Studio?

Issue

I have two application class which I want to combine in one, but provides two function, but I want it only one class so that I can call it on the application class in my manifest and get the App to produce both functions since i can not have two classes called on the application class in my manifest file in android studo.

I would like to put the AppController class in the App.Java class

Where I am confused is how to combine it since both extends different classes which java does not permit extending two classes in one.

Below is the App.java class

public class App extends MultiDexApplication implements Constants {

    public static final String TAG = App.class.getSimpleName();

    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    private static App mInstance;

    private ArrayList<Feeling> feelingsList;
    private ArrayList<BaseGift> giftsList;

    private SharedPreferences sharedPref;

    private String username, fullname, accessToken, gcmToken = "", fb_id = "", photoUrl, coverUrl, area = "", country = "", city = "";
    private Double lat = 0.000000, lng = 0.000000;
    private long id;
    private int state, allowRewardedAds = 1, admob = 1, ghost, pro, verify, balance, allowShowMyInfo, allowShowMyFriends, allowShowMyGallery, allowShowMyGifts, allowGalleryComments, allowComments, allowMessages, allowLikesGCM, allowCommentsGCM, allowFollowersGCM, allowGiftsGCM, allowMessagesGCM, allowCommentReplyGCM, errorCode, currentChatId = 0, notificationsCount = 0, messagesCount = 0, guestsCount = 0, newFriendsCount = 0;

    @Override
    public void onCreate() {

        super.onCreate();
        mInstance = this;

        FirebaseApp.initializeApp(this);

        sharedPref = this.getSharedPreferences(getString(R.string.settings_file), Context.MODE_PRIVATE);

        this.readData();

        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {

            try {

                ProviderInstaller.installIfNeeded(this);

            } catch (Exception e) {

                e.getMessage();
            }
        }
    }

   // NB; I have some more codes in here which i am unable to put in full here.
    }
}

Then this this the second class AppController.java

public class AppController extends Application {

    private static Context mContext;
    private static String mAppUrl;
    public static MediaPlayer player;
    public static Activity currentActivity;
    @Override
    public void onCreate() {
        super.onCreate();
        setContext(getApplicationContext());
        mAppUrl = Constant.PLAYSTORE_URL + mContext.getPackageName();
        setTelephoneListener();
        player = new MediaPlayer();
        mediaPlayerInitializer();
        //AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    public static void mediaPlayerInitializer(){
        try {
            player = MediaPlayer.create(getAppContext(), R.raw.snd_bg);
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.setLooping(true);
            player.setVolume(1f, 1f);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    public static String getAppUrl() {
        return mAppUrl;
    }
    private static void setContext(Context context) {
        mContext = context;
    }
    public static Context getAppContext() {
        return mContext;
    }

    
    public static void playSound() 
    {
        try {
            if (SettingsPreferences.getMusicEnableDisable(mContext)&&!player.isPlaying()) {
                player.start();
            }else{
            }

        } catch (IllegalStateException e) {
            e.printStackTrace();
            mediaPlayerInitializer();
            player.start();
        }
    }
    public static void StopSound() {
        if (player.isPlaying()) {
            player.pause();
        }

    }
    
    private void setTelephoneListener() {
        PhoneStateListener phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    StopSound();
                } else if (state == TelephonyManager.CALL_STATE_IDLE) {
                } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    StopSound();
                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };

        TelephonyManager telephoneManager = (TelephonyManager) getAppContext().getSystemService(Context.TELEPHONY_SERVICE);
        if (telephoneManager != null) {
            telephoneManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    static
    {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }
}

Solution

MulitdexApplication extends Application.
You could change your AppController to:

public class AppController extends MulitdexApplication 

and then your App to

public class App extends AppController implements Constants

Answered By – Traendy

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published