Playing audio from different class? - java

I have 15 or so activities. Each one of them has a method, and I want to play audio in that method. Now, I have the obvious option to copy and paste the following line of code into each and every one of my activities Now, if I wanted to change something, I would have to go back into each and every one of my activities again. Here is the code:
MediaPlayer pop = MediaPlayer.create(CurrentActivity.this, R.raw.pop);
pop.start();
So, after searching the web for a few hours, I found that most people would just copy and paste it into each activity. So, I put the line of code (above) into a separate java class (which was a service by the way) tried to call that method in the service every time I needed to play the audio. So, I did the following:
public class TwentySeconds extends Service{
public void myPop(View view){
MediaPlayer pop = MediaPlayer.create(TwentySeconds.this, R.raw.pop);
pop.start();
}
}
Now, I got the error non static method cannot be referenced from static context. So, naturally, I tried to make method myPop static. Then, I got the error on TwentySeconds.this about being referenced from static context. So, it seems I am stuck. Changing the methods to static can't work, as I am trying to use an instance of the class as well using this. So, how should I go about calling method myPop where the MediaPlayer can successfully play?
Thanks for the advice,
Rich

Typically, if a utility method needs a Context, it is passed in.
public class Utilities {
public static void myPop(Context context){
MediaPlayer pop = MediaPlayer.create(context, R.raw.pop);
pop.start();
}
}
Utilities.myPop(CurrentActivity.this);

Related

How to correctly implement stfalcon's chatkit

I'm trying to implement a chat app using stfalcon's ChatKit library. I've followed the docs in their repo, but there are things I'm not sure I'm getting right.
First, I created a new activity called DialogsListActivity, and copied the xml in the activity's xml file.
From here I first copied the xml part to the activity's xml file.
Next comes the adapter setup. I copied the given code after the OnCreate method, including the last line (dialogsListView.setAdapter(dialogsListAdapter);) as the last line in OnCreate. The whole activity now looks like this:
ListView dialogsListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialogs_list);
dialogsListView.setAdapter(dialogsListAdapter);
}
DialogsListAdapter dialogsListAdapter = new DialogsListAdapter<>(dialogs, new ImageLoader() {
#Override
public void loadImage(ImageView imageView, String url) {
//If you using another library - write here your way to load image
Picasso.with(DialogsListActivity.this).load(url).into(imageView);
}
});
}
Questions:
is this the right place to put the adapter in?
is it ok to put set the dialogsListView as an attribute and defining it inside OnCreate()?
the dialogs from new DialogsListAdapter gets a Cannot resolve symbol 'dialogs' message.
the new ImageLoader() from same place gets a Class 'Anonymous class derived from ImageLoader' must either be declared abstract or implement abstract method 'loadImage(ImageView, String, Object)' in 'ImageLoader'
What am I missing there?
For the IDialog and IUser implementation I created the classes DefaultDialog and Author, and copied the given code. As I expected, the 'symbols' returned by the methods 'cannot be defined'. Where should they be defined and how?
Next in the tutorial is the Data management section which I think would set those values.
I already downloaded the sample project and tried to look inside, but I cannot find the public class DefaultDialog that implements IDialog or anything similar. Plus, I got pretty lost trying to understand the library from that sample project.
Any help would be much appreciated.
Thanks in advance.

Singleton for multiple uses?

I am developing an android application in which I can/allowed to use only one instance of MediaPlayer(Singleton instance).
static MediaPlayer mp;
public static MediaPlayer getMediaPlayer() {
if (mp == null) {
mp = new MediaPlayer();
}
return mp;
}
Now This activity can be called for two purposes
from within application (Say ABC)
from any other application (say XYZ) for preview
This instance of mediaplayer is supposed to play either video or audio.
So I have created a class which handles request(parameterized constructor to check for audio instance needed or video instance needed) and based on request, it creates required instance.
But the problem arises when Within application user is playing an audio file and user launched other application (XYZ) and requested to play video.
Now I am storing MediaPlayer's previous state (like which file it was playing and current position etc) and releasing MediaPlayer to be used for XYZ application for Video Playing. And once user stops playing video, ABC resumes playing audio file based on the stored instance.
So is this the right approach? Or I need to do modify the architecture of this task?
Please suggest w.r.t. design patterns
Using singletons in Android for state persistance is something to be careful about.
Mabye you should investigate the activity lifecycle to understand in which cases you can loose the singleton instance (and therefore all the data stored inside it).
Here is some information
For instance when your app is running in the background, the cellphone can ask for memory and kill the instance of your singleton.
Here is an example Singleton that I would try:
public class MediaPlayer{
private static MediaPlayer instance = new MediaPlayer();
public static MediaPlayer getInstance() {
return MediaPlayer.instance;
}
}
Now you have a scenario in which you can create objects based on the getInstance() method which can only reference the same object which will ensure your singleton pattern. I tested it and attempted to make objects from this singleton pattern:
MediaPlayer t1 = MediaPlayer.getInstance();
MediaPlayer t2 = MediaPlayer.getInstance();
System.out.println(t1.equals(t2));
result:
true

Inject Context to activity

I am combining a static code analysis with a dynamic one. I basically create a new activity and set it up as the starting activity. During the run various methods in existing activities should be called.
I can call e.g. onCreate from outside, however, the super call to Activity will fail (or calls to SharedPreferences or other interesting classes) since Android does some initialization stuff when using the intents in order to call an activity (e.g. setting the context). But I need to somehow call methods like onCreate or onPause from outside while giving the target activity a valid context.
In my newly created activity I have got a valid context. I tried to pass it via calling ContextWrapper.attachBaseContext, but there is still a NullPointerException somewhere in Android due a the missing context. Is there some way to hack this somehow into a working state? Using reflection or other hacks would be no problem, since it is for analysis purposes only.
Thank you very much for any tips. I'd be able to modify the analyzed apps in any way to get this working.
However: Using an Intent is no option, since I cannot control which Activity-methods are being called, when and how often. I know that android has not been made for calling these methods directly, but it is not a common use case either :);
I have created a hack, which seems to help (I can get a valid context in the hacked activity). Let's see how far I get using this.
public static void hack(Activity hack, Activity main) {
try {
Field mActivityInfo = getField(Activity.class, "mActivityInfo");
mActivityInfo.set(hack, getClass("android.content.pm.ActivityInfo").newInstance());
Field mFragments = getField(Activity.class, "mFragments");
Field mContainer = getField(Activity.class, "mContainer");
Field mApplication = getField(Activity.class, "mApplication");
Field mWindow = getField(Activity.class, "mWindow");
Class FragmentManagerImpl = getClass("android.app.FragmentManagerImpl");
FragmentManager manager = (FragmentManager) mFragments.get(hack);
mApplication.set(hack, main.getApplication());
mWindow.set(hack, main.getWindow());
Class<?> FragmentContainer = getClass("android.app.FragmentContainer");
Method attachActivity = getMethod(FragmentManagerImpl, "attachActivity", Activity.class, FragmentContainer, Fragment.class);
attachActivity.invoke(manager, hack, mContainer.get(hack), null);
Method attachBaseContext = getMethod(ContextWrapper.class, "attachBaseContext", Context.class);
attachBaseContext.invoke(hack, new HackContext(main));
System.out.println("Hack performed");
} catch (Exception e) {
e.printStackTrace();
System.err.println("Hack failed :(");
}
}

Static Context from Activity.onStart()

I am trying to generate a notification from a class, Utilities.java, outside of the subclass of Context. I've thought about providing a SingletonContext class and have looked at posts ike this. I'd like to be able to return != null Context object since the notification can be generated at any given time because it is generated from a messageReceived() callback.
What are there downsides to doing something like this:
public static Context c;
public class MainActivity extends Activity{
#Override
public void onStart()
super.onStart()
c = this.getApplicationContext();
}
//other method somewhere outside this class
public Context getContext(){
return MainActivity.c
}
I don't think it would be any different than putting this on the onCreate(), however, it guarantees that the context is up to date when the activity starts.
The Context keeps a reference to this activity in memory, which you might not want. Perhaps use
this.getApplicationContext();
instead. This will still let you do file IO and most other things a context requires. Without a specific reference to this activity.
Maybe you should overwrite the onResume Method.
If you open a new activity, and switch back, the onStart method will not getting invoked.
Android Lifecycle: doc
BTW: I read about problems with ApplicationContext using a dialog or toast, so if you use the context to create on of these you should use your Activity as context.

Generic classes in my application

I've created a few minor apps for Android while learning. Being a PHP developer, it's a challenge to get used to it.
I'm especially wondering how I could define a couple of "general" functions in a separate class. Eg I have a function that checks if network connection is available, and if not, shows a dialog saying that the user should enable it. Currently, that function exists in several of my activities. Of course that seems strange - I suppose it would be more logical to define it once and include it in the activites where needed.
I tried putting it in a new class, and included that class in the original activity. But that failed since eg getBaseContext() is not accepted anymore.
I'm wondering how to go ahead. What should I be Google-ing for ? What is this mechanism called?
You need to create class with static methods. Like this
public class HelperUtils {
public static void checkNetworkConnection(Context ctx) {...}
}
Then you can call it from any place like this:
HelperUtils.checkNetworkConnection(this.getContext());
Assuming current class has Context.
You should read books on general OOP concepts where different type of methods are explained.
You can for example create a class - let's call it NetworkUtils. In this class you can create static method boolean isNetworkConnectionAvailable() and return true if is available and false otherwise. In this class you can create another static method void showNoConnectionDialog(Activity activity) - and in this method you create dialog starting with
public static void showNoConnectionDialog(Activity activity) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//setting message, listener etc. and finally
builder.create().show();
}
In your activity, where you want to check and handle network connection you should call:
if (!NetworkUtils.isConnectionAvailable(getApplicationContext())) {
NetworkUtils.showNoConnectionDialog(YourActivityClassName.this)
}
I guess this should work.

Categories

Resources