Android 创建其它应该程序的上下文对象

// Context.CONTEXT_IGNORE_SECURITY 忽略安全检测
        //getContext().createPackageContext 创建其它应该程序的上下文对象
        Context otherContext= getContext().createPackageContext("cn.itcast.preferences", Context.CONTEXT_IGNORE_SECURITY);
        SharedPreferences prefence= otherContext.getSharedPreferences("itcast", Context.MODE_PRIVATE);
        
        String nameString= prefence.getString("name", "侯志敏");
        int ageInt= prefence.getInt("age", 25);
        Log.i(TAG, "name:"+nameString+"========age:"+ageInt);

原文地址:https://www.cnblogs.com/soundcode/p/2506838.html