作者:飞扬的青春2046是我 | 来源:互联网 | 2023-05-17 12:30
importorg.crazyit.other.R;importandroid.app.Activity;importandroid.content.Context;importandr
import org.crazyit.other.R;
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 ReadOtherPreferences extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Context useCount = null;
try
{
// 获取其他程序所对应的Context,这里非常关键,是读取别的程序的包明!
useCount = createPackageContext("org.crazyit.io",
Context.CONTEXT_IGNORE_SECURITY);
}
catch (NameNotFoundException e)
{
e.printStackTrace();
}
// 使用其他程序的Context获取对应的SharedPreferences
SharedPreferences prefs = useCount.getSharedPreferences("count",
Context.MODE_WORLD_READABLE);
// 读取数据
int count = prefs.getInt("count", 0);
TextView show = (TextView) findViewById(R.id.show);
// 显示读取的数据内容
show.setText("UseCount应用程序以前被使用了" + count + "次。");
}
}