Android 4.0访问第三方的sharedPreferences数据

首先把国笔输入法生成的sharedPreferences文件拷贝出来,查看显示全屏与非全屏的关键字。

但是2.3系统使用

try {
otherAppsContext=createPackageContext("com.guobi.gbime", CONTEXT_IGNORE_SECURITY );
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
Log.i("guobi", e.getMessage());
}

SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("com.guobi.gbime_preferences", Context.MODE_WORLD_READABLE);

String name2 = sharedPreferences.getString("HandwriteFrameType", "");是可以获取到数据,而4.0就不行,打印的是空。

最终发现主要是权限问题。

国笔输入法生成的sharedPreferences文件是rw- rw- ---,,所以我们必须改变它的权限进行操作,其实目录也要改权限,都改为777。我在launcher的oncreate()方法中加入以下代码

//修改国笔输入法的显示模式,把全屏改为非全屏模式
             
      xmlFile = new File("/data/data/com.guobi.gbime/shared_prefs/com.guobi.gbime_preferences.xml");
      if(xmlFile.exists())
      {
      try {
              Runtime.getRuntime().exec(new String[]{"su","-c","chmod  777 /data/data/com.guobi.gbime/shared_prefs/com.guobi.gbime_preferences.xml"});
              Log.i("guobi", "chmod 777");
              Runtime.getRuntime().exec(new String[]{"su","-c","rm /data/data/com.guobi.gbime/shared_prefs/com.guobi.gbime_preferences.xml"});
                Log.i("guobi", " rm");
              Runtime.getRuntime().exec(new String[]{"su","-c","touch /data/data/com.guobi.gbime/shared_prefs/com.guobi.gbime_preferences.xml"});
                Log.i("guobi", " touch");
              Runtime.getRuntime().exec(new String[]{"su","-c","chmod  777 /data/data/com.guobi.gbime/shared_prefs/com.guobi.gbime_preferences.xml"});
                Log.i("guobi", " chomd");
           
      } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
        }
}
        else
        {
              if(!xmlFile.getParentFile().exists())
              {
                try {
                      Runtime.getRuntime().exec(new String[]{"su","-c","mkdir /data/data/com.guobi.gbime/shared_prefs"});
              Log.i("guobi", "mkdir");
Runtime.getRuntime().exec(new String[]{"su","-c","chmod  777 /data/data/com.guobi.gbime/shared_prefs"});
                Log.i("guobi", " chomd");


              } catch (IOException e) {
                      // TODO Auto-generated catch block
                    e.printStackTrace();
              }


                }
                       
      try {
              Runtime.getRuntime().exec(new String[]{"su","-c","touch /data/data/com.guobi.gbime/shared_prefs/com.guobi.gbime_preferences.xml"});
              Log.i("guobi", "touch");
              Log.i("guobi", "chmod  777");
              Runtime.getRuntime().exec(new String[]{"su","-c","chmod  777 /data/data/com.guobi.gbime/shared_prefs/com.guobi.gbime_preferences.xml"});
      } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
      }


      }


      try {
              otherAppsContext=mContext.createPackageContext("com.guobi.gbime",mContext. CONTEXT_IGNORE_SECURITY );
      } catch (NameNotFoundException e) {
              // TODO Auto-generated catch block
              Log.i("guobi", e.getMessage());
      }
                     
      SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("com.guobi.gbime_preferences", Context.MODE_WORLD_READABLE);
      {
        SharedPreferences.Editor guobieditor=sharedPreferences.edit();
          guobieditor.putString("HandwriteLang", "china");
          guobieditor.putString("HandwriteFrameType", "box");
          guobieditor.putString("IMType", "pinyin");
          guobieditor.putString("KeyboardType", "26");
          guobieditor.putString("HandwriteStrokeMask", "normal");
          guobieditor.putString("SkinType", "default");
          guobieditor.commit();
          String name2 = sharedPreferences.getString("HandwriteFrameType", "");
          Log.i("guobi", "HandwriteFrameType--"+name2);
      }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/5e8ff0deaf89dfd96a4000735f681afe.html