Preferences settings save open read write application Android example
Android app setup preferences, settings, open, read, write, onStop(), onPause(), getSharedPreferences(), SharedPreferences.Editor getInt(), getBoolean(), getString() , putInt(), putString(), putBoolean() example source code.
import android.content.SharedPreferences;
public class MainClass extends Activity {
public static final String PREFERENCES_NAME = "MyPrefsFile";
String myNewString;
int myNewInt;
Boolean myNewBool;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// read old settings if exist
SharedPreferences settings = getSharedPreferences(PREFERENCES_NAME, 0);
String sDefault = "Hello!";
String sStringFromPrefFile = settings.getString("myString", sDefault); // new text or default Hello!
int nDefaultIndex = 2; //
int nIndexFromPrefFile = settings.getInt("myInt", nDefaultIndex); // 4 or default 2
Boolean bDefault = false;
Boolean bFromPrefFile = settings.getBoolean("silentMode", bDefault); // true or default false
// new settings will saved in onStop or onPause
myNewString = "new text";
myNewInt = 4;
myNewBool = true;
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(getApplicationContext(), "onPause "
, Toast.LENGTH_SHORT).show();
savePreferences();
}
@Override
protected void onStop() {
super.onStop();
savePreferences();
}
/**save settings*/
public void savePreferences(){
try {
SharedPreferences settings = getSharedPreferences(PREFERENCES_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("myString", myNewString);
editor.putInt("nMyInt", myNewInt);
editor.putBoolean("silentMode", myNewBool); //
editor.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
396LW NO topic_id
AD
Další témata ....(Topics)
Tutorial by pictures how evaluate a variable in Eclipse debugger window.
1.) Open Debug perspective in Eclipse and to start debugging a Activity.
2.) Open Display window from menu Window->Show view->Display
3.) Set breakpoint where you need to evaluate a variable.
4.) Debug the Activity to breakpoint.
5.) Into the Display window type code for evaluate your variable and execute code.
6.) Check if change of value a variable
1.) Open Debug perspective in Eclipse and to start debugging a Activity.
2.) Open Display window from menu Window->Show view->Display
3.) Set breakpoint where you need to evaluate a variable.
4.) Debug the Activity to breakpoint.
5.) Into the Display window type code for evaluate your variable and execute code.
6.) Check if change of value a variable
Try each step separately:
- Restart Eclipse
- Clean project and rebuild
- Create a new project and try it if the problem persists, if no move copy project to other folder, delete project from workspace , create new project same name and copy java, xml etc. files to new project
- Close Eclipse, backup folder c:\Users\myName\workspace\.metadata and delete it. Restart Eclipse try again import project to workspace
- Re-installing the Android Developer Tools
- Re-installing Eclipse - rename old folder with Android to Android_old, create new folder C:\Program Files\Android and copy new Eclipse with sdk into new folder
- Created a new project importing from the file system
- Created a new project from subversion
Try this solution:
String DATA = "Html text....bla bla bla. Hellou world! čšřžěéá";
String HEADERHTML =
"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">"
+"<html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8">"
+"</head> <body>";
String FOOTERHTML = "</body></html>";
WebView mWebView.loadData(HEADERHTML+DATA+FOOTERHTML,"text/html; charset=UTF-8",null);
I You use offline work for better building speed of project or for other purpose
https://gradle.org/releases
Offline work:
File - Settings - Gradle
- select Use local gradle distribution
- select path Gradle home:
- check Offline work
https://gradle.org/releases
Offline work:
File - Settings - Gradle
- select Use local gradle distribution
- select path Gradle home:
- check Offline work
On Android device is path to *.apk like this example:
How get package *.apk path on device dynamically Android example
/data/app/cz.okhelp.my_package.apk
How get package *.apk path on device dynamically Android example
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String sPackagePath = getPackageResourcePath();
}
Editace: 2011-09-19 15:11:38
Počet článků v kategorii: 396
Url:preferences-settings-save-open-red-application-android-example