安卓 SharePreferences 跨进程获取数据

    在应用中, 如果存在看大图等功能, 会消耗很多内存, 开发者往往会想在大图界面新开一个进程, 来获取更多的内存空间. 然而在遇到跨进程获取SharePreferences 数据时往往会遇到问题, 本文介绍利用SharePreferences如何跨进程获取数据.


//直接代码

首先MainActivity与SecondActivity 不在同一个进程中设置清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dllo.thirdlogindemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity" android:process=":remote"/>
    </application>

</manifest>

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn1;
    private Button btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.btn1);//存储sp按钮
        btn2 = (Button) findViewById(R.id.btn2);//点击换进程按钮


        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);//向sp中传值
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("name", "张三").commit();
                break;
            case R.id.btn2:
                Intent intent = new Intent(this, SecondActivity.class);//点击跳转
                startActivity(intent);
                break;
        }
    }
}

public class SecondActivity extends AppCompatActivity {

    private Button btn3;
    private static final String TARGET_PACKAGE_NAME = "com.example.dllo.thirdlogindemo";// 目标数据程序的包名,
    /**
     * 根据目标程序的包名来获取其程序的上下文
     * @return
     * @throws
     */
    private Context getTargetContext() throws PackageManager.NameNotFoundException {
        return createPackageContext(TARGET_PACKAGE_NAME, Context.CONTEXT_IGNORE_SECURITY);
    }


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


        btn3 = (Button) findViewById(R.id.btn3);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                
                try {
                    SharedPreferences share = getTargetContext().getSharedPreferences("test", Context.MODE_WORLD_READABLE | Context.MODE_MULTI_PROCESS);
                    String name = share.getString("name", "默认");// 得到sp数据中的值
                    Log.d("SecondActivity~~~~~~~~~", name);
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                    Log.d("SecondActivity", "木有找到该包名");
                }
            }
        });
    }
    
    
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="存储sp" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击换进程" />
</LinearLayout>

activity_two.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">

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:layout_gravity="center"
        android:text="读值" />

</LinearLayout>

运行根据log就可以得到sp数据中的值啦!

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值