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

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

示例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)完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值