简体   繁体   English

Android 获取另一个应用程序的共享首选项

[英]Android Get Shared Preferences of Another application

I'm trying to get some data from Word Readable Shared Preferences of another application.我正在尝试从另一个应用程序的 Word 可读共享首选项中获取一些数据。

I'm sure that other application Shared Preferences is World Readable and name is present.我确定其他应用程序共享首选项是世界可读的并且存在名称。

But I'm retrieving empty string.但我正在检索空字符串。

Here is my code:这是我的代码:

package com.example.sharedpref;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.TextView;
import android.content.SharedPreferences;

public class MainActivity extends AppCompatActivity {
    private TextView appContent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        appContent = (TextView) findViewById(R.id.test1);
        appContent.setText("Test Application\n");
        appContent.setText(getName(this));
    }
    protected String getName(Context context){
        Context packageContext = null;
        try {
            packageContext = context.createPackageContext("com.application", CONTEXT_IGNORE_SECURITY);
            SharedPreferences sharedPreferences = packageContext.getSharedPreferences("name_conf", 0);
            String name = sharedPreferences.getString("name", "");
        return  name; //HERE IS WHERE I PUT BREAKPOINT
        }

        catch (PackageManager.NameNotFoundException e) {
            return null;

        }
    }
}

Here is some debug I retrieve from debug mode:这是我从调试模式中检索到的一些调试:

名称已编辑

and piece of code from the app that I'm trying to access shared preferences:以及我尝试访问共享首选项的应用程序中的一段代码:

    /* access modifiers changed from: protected */
    @SuppressLint({"WorldReadableFiles"})
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_welcome);
        Editor edit = getApplicationContext().getSharedPreferences("name_conf", 0).edit();
        edit.putBoolean("FIRSTRUN", false);
        edit.putString("name", "Eddie");
        edit.commit();
        new DBHelper(this).getReadableDatabase();

PS. PS。 I Cant edit code of second app im trying access to?我无法编辑我尝试访问的第二个应用程序的代码? Something im missing?我缺少什么?

You can not edit it from another apps , if you want, you can use FileWriter to create a txt file and use it in other apps不能从其他应用程序编辑它,如果需要,您可以使用 FileWriter 创建一个 txt 文件并在其他应用程序中使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM