访问外部程序的SharedPreference
项目1:创建sharedpreference文件,包名:com.shared.p1
注意1:SharedPreferences shared_f = getSharedPreferences("shared_f_name", Context.MODE_WORLD_READABLE);第二个参数MODE设置为Context.MODE_WORLD_READABLE或Context.MODE_WORLD_WRITEABLE
demo source:
[html]
package com.shared.p1;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Shared_f1Activity extends Activity implements OnClickListener {
Button button1, button2, button3;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences shared_f = getSharedPreferences("TEST", Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = shared_f.edit();
switch (v.getId()) {
case R.id.button1:
editor.putString("name", getResources().getString(R.string.name));
editor.commit();
break;
case R.id.button2:
editor.putInt("age", Integer.parseInt(getResources().getString(R.string.age)));
editor.commit();
break;
case R.id.button3:
String name = shared_f.getString("name", "De易做图t People");
int age = shared_f.getInt("age", 0);
Toast.makeText(Shared_f1Activity.this, name+":"+age, Toast.LENGTH_SHORT).show();
break;
}
}
}
布局文件只有3个按钮
项目2:可以访问项目1的sharedPreference文件 ,package com.shared.p1;
访问项目1的sharedPreference文件,必须获得项目1的context--方法如下:
f1Context = createPackageContext("com.shared.p1", Context.CONTEXT_IGNORE_SECURITY);
demo2 source
[html]
package com.shared.f2;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Shared_f2Activity extends Activity implements OnClickListener{
Button button1;
private Context f1Context;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
try {
f1Context = createPackageContext("com.shared.p1", Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.button1:
SharedPreferences sharedPreferences = f1Context.getSharedPreferences("TEST", Context.MODE_WORLD_READABLE);
String name = sharedPreferences.getString("name", "defValue");
int age = sharedPreferences.getInt("age", 0);
button1.setText(name+":"+age);
 
补充:综合编程 , 其他综合 ,