替代加preferencesFromResource为德precated [英] Alternative to addPreferencesFromResource as its deprecated

查看:621
本文介绍了替代加preferencesFromResource为德precated的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建我的应用程序preference活动,允许用户启动/停止背景闪屏音乐如下:

I create Preference activity on my app to allow user to start/stop background splash screen music as follow :

public class Prefs extends PreferenceActivity{

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);    
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);


    }    
}

和XML文件夹中创建prefs.xml:

and inside xml folder create prefs.xml :

 <?xml version="1.0" encoding="utf-8" ?> 
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
      <CheckBoxPreference 
         android:title="splash music" 
         android:defaultValue="true" 
         android:key="checkbox" 
         android:summary="Plese remove music "/>
    </PreferenceScreen>

这code飞溅活动:

and this code for splash activity :

 public class Splash extends Activity{  
    MediaPlayer ourSong;
@Override
protected void onCreate(Bundle Drandroid) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    // TODO Auto-generated method stub
    super.onCreate(Drandroid);
         setContentView(R.layout.splash);  

    ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound); 

     SharedPreferences getPrefs = 
               PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean music = getPrefs.getBoolean("checkbox", true);
    if (music == true)      
    ourSong.start();

    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(5000); }
              catch (InterruptedException e){
                e.printStackTrace(); }
              finally{
    Intent openTurkeyTrip = new Intent("com.android.dr.MENU");

        startActivity(openplanet); }}                                   
                                };
         timer.start();   }

@Override
protected void onPause() {
            // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
    finish();
          } 
       }

如何解决它是不是去precated也是我的应用程序将支持新老设备等类,如下:

how can i solve it with other class which is not deprecated also my app will support old and new devices as below :

  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

任何意见将AP preciated,谢谢。

any advice will be appreciated, thanks.

推荐答案

preferenceActivity 不是去precated。

PreferenceActivity is not deprecated.

添加preferencesFromResource() preferenceActivity 的德precated。但是,如果要支持API级别10或更低,你没有选择,只能使用它,至少在那些设备。

addPreferencesFromResource() on PreferenceActivity is deprecated. However, if you are supporting API Level 10 or lower, you have no choice but to use it, at least on those devices.

pcated方法的非德$ P $是使用 preferenceFragment 连同 preferenceActivity ,如描述了 prefereceActivity 文档。如果您的应用程序只支持API级别11或更高,只是使用它。如果您的应用程序支持旧设备,无论是:

The non-deprecated approach is to use PreferenceFragment in conjunction with PreferenceActivity, as is described in the PrefereceActivity documentation. If your app is only supporting API Level 11 and higher, just use that. If your app is supporting older devices, either:

  • 使用添加preferencesFromResource()所有的时间,因为它仍然有效,直到你放弃对旧版本的支持,或

  • Use addPreferencesFromResource() all the time, as it still works, until you drop support for the older versions, or

使用添加preferencesFromResource()仅在旧设备(通过检查 Build.VERSION.SDK_INT ),并依靠新的片断为基础的系统上较新的设备

Use addPreferencesFromResource() only on the older devices (by checking Build.VERSION.SDK_INT), and rely on the new fragment-based system on newer devices

这篇关于替代加preferencesFromResource为德precated的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆