上一篇博客中讨论了在本应用程序中使用SharedPreferences数据,现在来讨论下使用其他应用程序中的SharedPreferences数据。
使用其他应用程序的SharedPreferences数据步骤
1、创建其他应用程序对应的Context
Context context = createPackageContext("其他应用程序的包名,如com.example.demosharedpreferences",Context.CONTEXT_IGNORE_SECURITY);
2、获取SharedPreferences对象
SharedPreferences sp = context.getSharedPreferences(String name,int mode);
3、进行相应的读写操作(与在本应用程序中使用SharedPreferences数据一样),请参看上一篇博客的讲解。
以下为小示例
第一个应用程序(DemoSharedPreferences)
count_activity.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
com.example.demosharedpreferences.UserCountActivity.java代码
package com.example.demosharedpreferences;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
public class UserCountActivity extends Activity {
private SharedPreferences sp;
private SharedPreferences.Editor editor;
private TextView tv_count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.count_activity);
tv_count = (TextView) findViewById(R.id.tv_count);
sp = getSharedPreferences("count", MODE_WORLD_READABLE);
editor = sp.edit();
int useCount = sp.getInt("useCount", 0);
editor.putInt("useCount", ++useCount);
editor.commit();
tv_count.setText("该Activity被访问的次数为:" + useCount);
}
}
注意:不要忘记了在AndroidManifest.xml中注册UserCountActivity
第二个应用程序(DemoSharedPreferencesExport)
activity_main.xml布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</RelativeLayout>
com.example.demosharedpreferencesexport.MainActivity.java代码
package com.example.demosharedpreferencesexport;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private Context useCountActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
useCountActivity = createPackageContext("com.example.demosharedpreferences", Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
// 使用其他程序的context来获取对应的SharedPreferences
SharedPreferences sp = useCountActivity.getSharedPreferences("count", MODE_WORLD_READABLE);
// 读取数据
int useCount = sp.getInt("useCount", 0);
TextView tv_show = (TextView) findViewById(R.id.tv_show);
tv_show.setText("其他应用程序的useCountActivity使用次数为:" + useCount);
}
}