android - Alternative to addPreferencesFromResource as its deprecated -
i create preference activity on app allow user start/stop background splash screen music 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); } }
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>
and code 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(); } }
how can solve other class not deprecated app support old , new devices below :
<uses-sdk android:minsdkversion="8" android:targetsdkversion="17" />
any advice appreciated, thanks.
preferenceactivity
not deprecated.
addpreferencesfromresource()
on preferenceactivity
is deprecated. however, if supporting api level 10 or lower, have no choice use it, @ least on devices.
the non-deprecated approach use preferencefragment
in conjunction preferenceactivity
, described in the prefereceactivity
documentation. if app supporting api level 11 , higher, use that. if app supporting older devices, either:
use
addpreferencesfromresource()
time, still works, until drop support older versions, oruse
addpreferencesfromresource()
on older devices (by checkingbuild.version.sdk_int
), , rely on new fragment-based system on newer devices
Comments
Post a Comment