论坛首页 移动开发技术论坛

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

浏览 3237 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-03-16  
                                如何访问外部应用程序的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)完成。
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics