最新消息:20210917 已从crifan.com换到crifan.org

【未解决】rcsjta项目编译出错:警告deprecation PreferenceActivity中的getPreferenceScreen()已过时

Preference crifan 448浏览 0评论
折腾:
【未解决】Android Studio中gradle编译rcjsta项目多个使用或覆盖了已过时的API等报错
期间,继续尝试解决:
rcsjta/samples/api/tts/src/com/orangelabs/rcs/tts/Main.java:76: 警告: [deprecation] PreferenceActivity中的getPreferenceScreen()已过时
        activateCheck = (CheckBoxPreference) getPreferenceScreen().findPreference("activate");
                                             ^
代码是:
activateCheck = (CheckBoxPreference) getPreferenceScreen().findPreference("activate");
PreferenceActivity  |  Android 开发者  |  Android Developers
getPreferenceScreen()
This method was deprecated in API level 15. This function is not relevant for a modern fragment-based PreferenceActivity.
Gets the root of the preference hierarchy that this activity is showing.
deprecation PreferenceActivity getPreferenceScreen
deprecated PreferenceActivity getPreferenceScreen
android – Deprecated Method: getPreferenceScreen – Stack Overflow
java – Non Deprecated findPreference() Method? – Android – Stack Overflow
android – Preference Activity Deprecated Methods – Stack Overflow
deprecated  android getPreferenceScreen
android  getPreferenceScreen 过期
android – getPreferenceScreen() – Stack Overflow
换成:getPreferenceManager
activateCheck = (CheckBoxPreference) getPreferenceManager().findPreference("activate");
去看看具体实现
android/preference/PreferenceActivity.java
/**
 * Returns the {@link PreferenceManager} used by this activity.
 * @return The {@link PreferenceManager}.
 *
 * @deprecated This function is not relevant for a modern fragment-based
 * PreferenceActivity.
 */
@Deprecated
public PreferenceManager getPreferenceManager() {
    return mPreferenceManager;
}
也提示Deprecated过期了
是有:
/**
 * Used to help create {@link Preference} hierarchies
 * from activities or XML.
 * <p>
 * In most cases, clients should use
 * {@link PreferenceActivity#addPreferencesFromIntent} or
 * {@link PreferenceActivity#addPreferencesFromResource(int)}.
 * 
 * @see PreferenceActivity
 */
public class PreferenceManager {

/**
 * Finds a {@link Preference} based on its key.
 * 
 * @param key The key of the preference to retrieve.
 * @return The {@link Preference} with the key, or null.
 * @see PreferenceGroup#findPreference(CharSequence)
 */
public Preference findPreference(CharSequence key) {
    if (mPreferenceScreen == null) {
        return null;
    }
    
    return mPreferenceScreen.findPreference(key);
}
重新编译看看效果
结果类似错误:
rcsjta/samples/api/tts/src/com/orangelabs/rcs/tts/Main.java:77: 警告: [deprecation] PreferenceActivity中的getPreferenceManager()已过时
        activateCheck = (CheckBoxPreference) getPreferenceManager().findPreference("activate");
                                             ^
android  getPreferenceManager 过期
Preference  |  Android 开发者  |  Android Developers
getPreferenceManager
Added in API level 1 

Deprecated in API level 29
public PreferenceManager getPreferenceManager ()
Gets the PreferenceManager that manages this Preference object's tree.
但是显示api 大于29才会过期
而此处项目中,sdk只有23,按道理不会过期才对:
build.gradle
ext {
    compileSdkVersion = 23
    minSdkVersion = 12
    targetSdkVersion = 23
}
android  getPreferenceManager Deprecated
android – getPreferenceManager deprecated – Stack Overflow
java – How to correct deprecated preference methods? – Stack Overflow
有人弄了个库:
saik0/UnifiedPreference: A convinience library for working with all versions of the Android Preference package from API v4 and up
android – How to create a PreferenceScreen in a PreferenceActivity? – Stack Overflow
放在PreferenceFragment中用getPreferenceManager
去移动过去:
public class Main extends PreferenceActivity implements Preference.OnPreferenceChangeListener {


    private CheckBoxPreference activateCheck;
    /**
     * List of permissions needed for service Just need to ask one permission per dangerous group
     */
    private static final Set<String> sAllPermissionsList = new HashSet<>(
            Arrays.asList(Manifest.permission.READ_CONTACTS));


    private static final int MY_PERMISSION_REQUEST_ALL = 5428;
    private Set<String> mPermissionsToAsk;


    public static class MyPreferenceFragment extends PreferenceFragment
    {
        @Override
        public void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.tts_preferences);


            activateCheck = (CheckBoxPreference) getPreferenceManager().findPreference("activate");
            activateCheck.setOnPreferenceChangeListener(this);
        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        askPermissions();
        // Set title
        setTitle(R.string.app_title);


        // Set preferences
//        addPreferencesFromResource(R.xml.tts_preferences);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();


//        activateCheck = (CheckBoxPreference) getPreferenceScreen().findPreference("activate");
//        activateCheck.setOnPreferenceChangeListener(this);
    }
但是发现:
全局变量activateCheck却无法直接赋值了
PreferenceFragment  |  Android 开发者  |  Android Developers
官网说了:
This class is deprecated.
Use PreferenceFragmentCompat instead
建议换PreferenceFragmentCompat
PreferenceFragmentCompat  |  Android 开发者  |  Android Developers
A PreferenceFragmentCompat is the entry point to using the Preference library. This Fragment displays a hierarchy of Preference objects to the user. It also handles persisting values to the device.
期间去:
【已解决】rcsjta中rcs/tts/Main.java和原先3个apk即core和RI和settings的关系
-》所以,看起来:即使代码写错了,暂时也影响不大。
当然,尽量还是代码正常为好。
那就大胆的去用PreferenceFragmentCompat改造代码
万一真的出错,到时候再去调试即可
所以现在接着去:
【未解决】Android Studio中用PreferenceFragmentCompat实现rcsjta的tts的参数设置页面

转载请注明:在路上 » 【未解决】rcsjta项目编译出错:警告deprecation PreferenceActivity中的getPreferenceScreen()已过时

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.201 seconds, using 19.91MB memory