Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing my custom class and calling it's method?

I have created a custom class for my Android project called "Sounds" I want to be able to call it from my activity. The contents of my class are as follows:

package com.mypackage;

import java.util.HashMap;

import android.content.Context;
import android.media.SoundPool;

public class Sounds {

private static boolean sound = true;

private static final int FLIP_SOUND = 1;

private static Context context;
private static SoundPool soundPool;
private static HashMap<Integer, Integer> soundPoolMap;

public static void initSounds() {
    soundPoolMap.put(FLIP_SOUND, soundPool.load(context, R.raw.flip, 1));
}

public static void playFlip() {
        soundPool.play(soundPoolMap.get(FLIP_SOUND), 1, 1, 1, 0, 1);
}

public static void setSound(Boolean onOff) {
    sound = onOff;
}
}

In my main Activity class I have tried importing the class, creating an instance of it but I guess I'm just not understanding how it's done. Can anybody point me in the right direction please?

like image 252
Hamid Avatar asked Oct 11 '10 14:10

Hamid


People also ask

How do I import a class I made in Java?

Otherwise, it is very easy. In Eclipse or NetBeans just write the class you want to use and press on Ctrl + Space . The IDE will automatically import the class.

Do you have to import classes from the same package?

You don't have to import classes which are in the same package. Show activity on this post. Well, classes in the same package are automatically imported.

What does importing a class do?

Importing classes from other programs allows us to use them within the current program. Thus, helping in improved readability and reusability of code. Importing can be done within the same or from different folders.


2 Answers

Edited: From your Activity class:

private Sounds s;

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);        
        s = new Sounds(this);
        s.initSounds();
}

You might also send the context with the constructor to your custom class.

Remove the static variables and methods:

public class Sounds {

private boolean sound = true;

private int FLIP_SOUND = 1;

private Context context;
private SoundPool soundPool;
private HashMap soundPoolMap;

public Sounds(Context context){
   this.context = context;
   soundPoolMap = new HashMap();
   soundPool = new SoundPool(0, AudioManager.STREAM_MUSIC, 0);
}

public void initSounds() {
   soundPoolMap.put(FLIP_SOUND, soundPool.load(context, R.raw.flip, 1));
}

public void playFlip() {
    soundPool.play(soundPoolMap.get(FLIP_SOUND), 1, 1, 1, 0, 1);
}

public void setSound(Boolean onOff) {
   sound = onOff;
}
}
like image 58
Wroclai Avatar answered Oct 08 '22 19:10

Wroclai


I've got also issues on this peculiar usage of classes. I'm a newbie in Android and even in the usage of classes and i am studing the WebInterface.

The Shane Oliver's solution worked for me using the standard class variables.

In the Activity class :

Private JavaScriptInterface myJavaScriptInterface;
myJavaScriptInterface.Toastshow("Hi EveryOne");

or even :

JavaScriptInterface myJavaScriptInterface = new JavaScriptInterface(this);

As for the class JavaScriptInterface :

 public class JavaScriptInterface {

    Context myContext;

    //Instanciar o interface e definir o conteudo 
    JavaScriptInterface(Context c) {
        myContext = c;
    }


    public void Toastshow(String toast_msg)
    {


        Toast.makeText(myContext, toast_msg, Toast.LENGTH_LONG).show();
    }
}

Hope this helps...

like image 44
malagas Avatar answered Oct 08 '22 17:10

malagas