Android中SharedPreferences的用法(二)

上一篇博客中讨论了在本应用程序中使用SharedPreferences数据,现在来讨论下使用其他应用程序中的SharedPreferences数据。

使用其他应用程序的SharedPreferences数据步骤
1、创建其他应用程序对应的Context

Context context = createPackageContext("其他应用程序的包名,如com.example.demosharedpreferences",Context.CONTEXT_IGNORE_SECURITY);

2、获取SharedPreferences对象

SharedPreferences sp = context.getSharedPreferences(String name,int mode);

3、进行相应的读写操作(与在本应用程序中使用SharedPreferences数据一样),请参看上一篇博客的讲解。

以下为小示例
第一个应用程序(DemoSharedPreferences)
count_activity.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView 
        android:id="@+id/tv_count"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

com.example.demosharedpreferences.UserCountActivity.java代码

package com.example.demosharedpreferences;


import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;

public class UserCountActivity extends Activity {

    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private TextView tv_count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.count_activity);
        tv_count = (TextView) findViewById(R.id.tv_count);

        sp = getSharedPreferences("count", MODE_WORLD_READABLE);
        editor = sp.edit();
        int useCount = sp.getInt("useCount", 0);
        editor.putInt("useCount", ++useCount);
        editor.commit();
        tv_count.setText("该Activity被访问的次数为:" + useCount);
    }
}

注意:不要忘记了在AndroidManifest.xml中注册UserCountActivity

第二个应用程序(DemoSharedPreferencesExport)
activity_main.xml布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        />

</RelativeLayout>

com.example.demosharedpreferencesexport.MainActivity.java代码

package com.example.demosharedpreferencesexport;

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 MainActivity extends Activity {

    private Context useCountActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            useCountActivity = createPackageContext("com.example.demosharedpreferences", Context.CONTEXT_IGNORE_SECURITY);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
//      使用其他程序的context来获取对应的SharedPreferences
        SharedPreferences sp = useCountActivity.getSharedPreferences("count", MODE_WORLD_READABLE);
//      读取数据
        int useCount = sp.getInt("useCount", 0);
        TextView tv_show = (TextView) findViewById(R.id.tv_show);
        tv_show.setText("其他应用程序的useCountActivity使用次数为:" + useCount);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xyz_ok

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值