How to find a replacement for deprecated method?

丶灬走出姿态 提交于 2019-11-28 09:38:31

问题


I am new to Android programming.

I have been following a tutorial on creating a Music Player from this website

The tutorial calls for a setAudioStreamType method from MediaPlayer Class which is deprecated. Which method replaces this deprecated method in particular? And, is there a source where we can find all deprecated methods and its current alternative?

Here is the code I have where there is a warning about a deprecated method:

public void  initMusicPlayer(){
    //set player properties
    player.setWakeMode(getApplicationContext(),
            PowerManager.PARTIAL_WAKE_LOCK);
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    player.setOnPreparedListener(this);
    player.setOnCompletionListener(this);
    player.setOnErrorListener(this);
}

回答1:


If you press CTRL+left click on a method, you find the method declaration. That is where a method is created with its contents and javadoc. Javadoc for deprecated methods often include a @deprecated annotation where it mentions the new API and when it was deprecated.

In addition to the javadoc information can also be found in the reference at developer.android.com.

Live example: the Camera class. At the end of the class javadoc it has this:

/**
 * (other declarations, and last)
 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
 *             applications.
 */

There you also get a link to the new alternative. If you check the reference at developer.android.com you can also see when it was deprecated (API) and (if you have a new class) you can see when that was introduced. Android has a lot to manage when it comes to API's and what was introduced when especially considering there is a new API each year.


As for the Javadoc solution, it can be followed as far as you want. If you have class X, but class X is deprecated. There is a javadoc link to class Y, which replaces class X. For some reason class Y is also deprecated. But there is a javadoc link there that leads to class Z.

Above was a basic example of how you can find the newest replacements, even though the replacement for class X is deprecated. Something to keep in mind in ANdroid programming though, is the fact that you may need the deprecated methods to support lower versions on Android.




回答2:


Use setAudioAttributes(AudioAttributes) instead of setAudioStreamType().

You can see these details in the Android Developer documentation.

https://developer.android.com/reference/android/media/MediaPlayer.html#setAudioStreamType(int)

To follow the latest changes, you can check the Android Developer blog: https://android-developers.googleblog.com/



来源:https://stackoverflow.com/questions/45404478/how-to-find-a-replacement-for-deprecated-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!