如何访问外部应用程序的SharedPreference

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何访问外部应用程序的SharedPreference相关的知识,希望对你有一定的参考价值。

参考技术A 示例AccessFromSharePreferenceDemo将说明如何读取其他应用程序(博客中另一项目:DemoSharedPreferences)中保存的SharedPreferences数据

1)新建android项目,项目名称:AccessFromSharePreferenceDemo
2)在继承自Activity的类中编写相应代码:
package com.mesada.demo1;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

/**
* This is a demo about access SharedPreferences.
*
* @author Xiaolong Long
* @date 2010-12-30
* @version 1.0
*/
public class MainActivity extends Activity
private static final String TAG = "MainActivity";
private static final boolean mIsPrintInfo = true;

public static final String PREFERENCE_PACKAGE = "com.mesada.demo";
public static final String PREFERENCE_NAME = "MyPrefsFile";
public static final String KEY_USERNAME = "USERNAME";
public static final String KEY_PWD = "PASSWORD";
public static int MODE = Context.MODE_WORLD_READABLE
+ Context.MODE_WORLD_WRITEABLE;

TextView mUserNameView;
TextView mPwdView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
if (mIsPrintInfo)
Log.i(TAG, "onCreate()...");

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupControlers();

Context context = null;
try
context = createPackageContext(PREFERENCE_PACKAGE,
Context.CONTEXT_IGNORE_SECURITY);
catch (NameNotFoundException e)
e.printStackTrace();

SharedPreferences settings = context.getSharedPreferences(
PREFERENCE_NAME, MODE);
String userName = settings.getString(KEY_USERNAME, "姚明");
String userPwd = settings.getString(KEY_PWD, "123456");
mUserNameView.setText(userName);
mPwdView.setText(userPwd);


/**
*
* Find the views that were identified by the id attributes from the XML.
*
* @param
* @return
* @date 2010-12-30
* @author Xiaolong Long
*/
private void setupControlers()
if (mIsPrintInfo)
Log.i(TAG, "setupControlers()...");

mUserNameView = (TextView) findViewById(R.id.userName);
mPwdView = (TextView) findViewById(R.id.password);



2.main.xml 如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lblusername" />
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lblpassword" />
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

3.AndroidMainfest.xml 文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mesada.demo1"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
4)完成。本回答被提问者和网友采纳

以上是关于如何访问外部应用程序的SharedPreference的主要内容,如果未能解决你的问题,请参考以下文章

如何在 vue.js 应用程序中访问外部 json 文件对象

如何创建代理来访问外部 API? [复制]

Qt Android,如何访问 QRunnable 中的外部对象?

如何访问组件外部的 react-redux 存储

如何从 main() 函数外部访问 argv[]?

如何从外部访问类内的自我对象