Android 中SharedPreferences跨应用读取数据的
https://download.csdn.net/download/gcsdn2000/4161520
保存
package edu.cczu.SimplePreference; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.widget.EditText; public class SimplePreferenceActivity extends Activity { private EditText nameText; private EditText ageText; private EditText heightText; public static final String PREFER_NAME = "SaveSet"; public static int MODE = Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); nameText = (EditText)findViewById(R.id.name); ageText = (EditText)findViewById(R.id.age); heightText = (EditText)findViewById(R.id.height); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); loadSharedPreferences(); } private void loadSharedPreferences() { // TODO Auto-generated method stub SharedPreferences share = getSharedPreferences(PREFER_NAME, MODE); String name = share.getString("Name", "Tom"); int age = share.getInt("Age", 20); float height = share.getFloat("Height", 1.81f); nameText.setText(name); ageText.setText(String.valueOf(age)); heightText.setText(String.valueOf(height)); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); saveSharedPreferences(); } private void saveSharedPreferences() { // TODO Auto-generated method stub SharedPreferences share = getSharedPreferences(PREFER_NAME, MODE); SharedPreferences.Editor editor = share.edit(); editor.putString("Name", nameText.getText().toString()); editor.putInt("Age", Integer.parseInt(ageText.getText().toString())); editor.putFloat("Height", Float.parseFloat(heightText.getText().toString())); editor.commit(); } }
读取其他应用数据
package edu.cczu.SharePreference; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import android.widget.Toast; public class SharePreferenceActivity extends Activity { public static final String PREFERENCE_PACKAGE = "edu.cczu.SimplePreference"; public static final String PREFERENCE_NAME = "SaveSet"; public static int MODE = Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE; private TextView labelView; private static String TAG = "LIFECYCLE"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Context c = null; labelView = (TextView)findViewById(R.id.label); try { c = this.createPackageContext(PREFERENCE_PACKAGE, Context.CONTEXT_IGNORE_SECURITY); } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } SharedPreferences sharedPreferences = c.getSharedPreferences(PREFERENCE_NAME, MODE); String name = sharedPreferences.getString("Name","Tom"); int age = sharedPreferences.getInt("Age", 20); float height = sharedPreferences.getFloat("Height",1.81f); String msg = ""; msg += "姓名:" + name + "\n"; msg += "年龄:" + String.valueOf(age) + "\n"; msg += "身高:" + String.valueOf(height) + "\n"; labelView.setText(msg); Toast.makeText(this, name, Toast.LENGTH_SHORT).show(); Log.i(TAG, "(1) onCreate()"); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); Log.i(TAG, "(2) onStart()"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); Log.i(TAG, "(8) onStop()"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.i(TAG, "(9) onDestroy()"); //System.exit(0); } }
最后更新:2017-04-03 12:55:04
上一篇:
Cmd批处理替换文件
下一篇:
2014年的阿里云产品
应该知道的Linux技巧
magento -- 推荐插件 -- 购物车预览 --Cart items preview on mouseover
阿里云服务器购买流程,阿里云ECS服务器申请步骤
HTAP数据库 PostgreSQL 场景与性能测试之 9 - (OLTP) 字符串模糊查询 - 含索引实时写入
IT人怎么赶走不快乐的心情?
上云,让业务尽情拥抱互联网:阿里云在企业专有云与混合云最佳实践
第一个Hello world(1)
在人工智能时代,我们更需要理解自己的智能 | 艾伦脑科学研究所所长STEP峰会演讲实录
Struts2中的ActionContext.getContext().getParameters()
使用MaxCompute快速实现企业数据化运营