您的位置:首页 > 移动开发 > Android开发

Android 中SharedPreferences跨应用读取数据的

2016-05-12 22:35 417 查看
http://download.csdn.net/download/gcsdn2000/4161520

保存

[java] view
plain copy







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();

}

}

读取其他应用数据

[java] view
plain copy







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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: