`
isiqi
  • 浏览: 16049804 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

Android中跨包访问Preferences

 
阅读更多
我们可以在一个应用程序中创建并编辑一个Preferences,然后在另外一个应用程序中读取它。当然有个前提是该<wbr>Preferences</wbr>的权限至少是Context.MODE_WORLD_READABLE
比如在包名为com.teleca应用程序A中有一个名叫com.teleca_prefer的Preferences:
final static String preferName="com.teleca_prefer";
final static String KEY_TIP="tip";
.......................................................
prefsWorldRead = getSharedPreferences(preferName,
Context.MODE_WORLD_READABLE);
Editor prefsWorldReadEditor = prefsWorldRead.edit();
prefsWorldReadEditor.putString(KEY_TIP, "Are you fine?");
prefsWorldReadEditor.commit();
我们可以在包名为com.teleca.robin应用程序B中这样读取它:
private SharedPreferences prefsWorldRead;
final static String preferName="com.teleca_prefer";
final static String KEY_TIP="tip";
....................................................................
if(prefsWorldRead ==null)
{
Context otherContext=null;
try {
otherContext =createPackageContext("com.teleca",Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
prefsWorldRead = otherContext.getSharedPreferences(preferName,
Context.MODE_WORLD_READABLE);
}
String tip=prefsWorldRead.getString(KEY_TIP, "null2");
createPackageContext为Context的方法,"com.teleca"为A应用程序的包名,"com.teleca_prefer"为你要读取的A应用程序的Preferences名字。

public abstractContextcreatePackageContext(StringpackageName, int flags)

Since:API Level 1

Return a new Context object for the given application name. This Context is the same as what the named application gets when it is launched, containing the same resources and class loader. Each call to this method returns a new instance of a Context object; Context objects are not shared, however they share common state (Resources, ClassLoader, etc) so the Context instance itself is fairly lightweight.

ThrowsPackageManager.NameNotFoundExceptionif there is no application with the given package name.

ThrowsSecurityExceptionif the Context requested can not be loaded into the caller's process for security reasons (seeCONTEXT_INCLUDE_CODEfor more information}.

Parameters
packageName flags
Name of the application's package.
Option flags, one ofCONTEXT_INCLUDE_CODEorCONTEXT_IGNORE_SECURITY.
Returns
  • A Context for the application.
Throws

PackageManager.NameNotFoundException
java.lang.SecurityException
if there is no application with the given package name
分享到:
评论

相关推荐

    Android代码-android-secure-preferences

    android-secure-preferences About This project uses the Encryption class from: http://www.java2s.com/Code/Android/Security/AESEncryption.htm Gives an implementation of SharedPreferences, which encrypts...

    Android代码-Multiplatform-Preferences

    Compatible with kotlin android and kotlin native for iphone class MyPresenter { val preferences = Preferences() fun start(){ preferences.getString("userName")?.let { view.displayUser(it) } val...

    android 数据存取Preferences

    他的用法基本上和J2SE(java.util.prefs.Preferences)中的用法一样,以一种简单、 透明的方式来保存一些用户个性化设置的字体、颜色、位置等参数信息。一般的应用程序都会提供“设置”或者“首选项”的这样的界面,...

    Android代码-rx-preferences

    Reactive SharedPreferences for Android. Usage Create an RxSharedPreferences instance which wraps a SharedPreferences: SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences...

    Android代码-一个快速方便地访问Android Shared preferences 库。

    FastSave is An Android library for fast and easy access to Android Shared preferences. It allows you to save any type or list in the sharedpreferences and retrieve it in convenient way. Installation ...

    Android Preferences

    Tutorial Android Preferences

    Android数据持久化之Preferences机制详解

    在Android中,实现数据持久化有五种方式:Preferences,文件File,I/O操作、SQLite数据库,ContentProvider组件。 下面逐个做一简单的介绍: 一、Preferences的介绍: Preferences是一种轻量级的数据存储机制,他将...

    Android 使用Shared Preferences进行数据存储-样例.rar

    Android 使用Shared Preferences进行数据存储-样例,演示如何使用 Shared Preferences 获得数据和保存数据、如何使用getPreferences方法创建文件的模式,以及如何使用getPreferences模拟用户参数设置、查看 ...

    Android代码-shared-preferences-helper

    Android Shared Preferences Helper Android Library to handle SharedPreferences boilerplate code and other tools Download dependencies { compile 'com.github.seanzor:shared-preferences-helper:1.1.0' }...

    Android代码-secure-preferences

    This is Android Shared preference wrapper that encrypts the values of Shared Preferences using AES 128, CBC, and PKCS5 padding with integrity checking in the form of a SHA 256 hash. Each key is stored...

    Preferences_android_

    Use of Preferences in Android pdf

    Android代码-preferences-helper

    preferences-helper SharePreferences is very popular with any project and all most all project has SharePreferences for saving data. This library will help you faster in configuration and use ...

    preferences

    preferences

    android 定制preferences布局和自定义对话框(左边带图标的preferences)

    很想做个天气预警的功能, 想用preferences来做界面。 看了很多preferences感觉定制性太差 所以自己做了一个。 应该是不错的demo 定制preferences在preferencesActivity中的布局 和自定义了对话框的布局

    android 数据存储之 Shared Preferences

    NULL 博文链接:https://byandby.iteye.com/blog/833292

    Preferences

    android设置一些常用的属性,如ip地址和端口号什么,都是可以用android的Preferences来设置,简单方便。

    Android-secure-preferences.zip

    Android-secure-preferences.zip,android共享首选项包装器加密共享首选项的值。这不是防弹安全,而是一个快速的胜利,逐步使你的安卓应用程序更安全。,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C...

    Android高手进阶教程与Android基础教程

    Android高手进阶教程之----Android中万能的BaseAdapter(Spinner,ListView,GridView)的使用!.doc Android高手进阶教程之----通过Location获取Address的使用.doc Android基础教程之----Android ProgressBar的使用.doc...

    关于android的数据存储-SQLite-ContentProvider-preferences

    关于android的数据存储-SQLite-ContentProvider-preferences

    Android代码-KotlinPreferences

    Kotlin Android Library, that makes preference usage simple and fun. KotlinPreferences now have a brother. With KotlinPreferences, you can define different preference fields this way: var ...

Global site tag (gtag.js) - Google Analytics