Android SharedPreferences读取外部程序的SharedPreferences存储
2015-09-14 17:05
176 查看
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 { <span style="color:#ff0000;"> <strong> // 获取其他程序所对应的Context,这里非常关键,是读取别的程序的包明!</strong></span> 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 + "次。"); } }
相关文章推荐
- Android SharedPreferences的使用
- 开源中国 OsChina Android 客户端源码分析(9)下载APK功能
- Android Studio五--Gradle命令与第三方包
- Android Studio四--Gradle基础
- Android 自定义控件---SpreadView
- Android系统版本与API Level对照表
- Android Studio二--基本设置与运行
- Android Canvas绘图详解(图文)
- Android自定义圆形进度条
- Android Sqlite 导入外部数据库
- Android中的自定义圆形头像
- Android绘图深度解析
- Android(java)学习笔记247:ContentProvider使用之利用ContentProvider备份和还原手机短信(掌握)
- ADB+adbWireless 抛开USB调试,接入无线调试
- Error:Execution failed for task ':sample:dexDebug'. > com.android.ide.common.process.ProcessExceptio
- 如何一步步实现在Android平台下运用OpenGl
- Android SQLite使用
- Android Makefile 文件讲解
- Android自定义View原理详解02
- android 自定义View原理详解01