Android 中SharedPreferences跨应用读取数据的

http://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);
	}    
}


  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值