安卓 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数据中的值啦!

 
  

你可能感兴趣的:(安卓 SharePreferences 跨进程获取数据)