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?
Try
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 :
or even :
As for the class JavaScriptInterface :
Hope this helps...
Edited: From your
Activity
class:You might also send the context with the constructor to your custom class.
Remove the static variables and methods:
You have made all the methods of the class static. If you want to use them as-is, call them with
Sounds.initSound()
and so on. However, since you have class variables, static methods and variables don't look appropriate. Removestatic
from your members (except fromFLIP_SOUND
) and then try creating an instance of the class and calling methods like normal.