不同程序之間不允許共享SharedPreferences。其他程序訪問不了該程序的SharedPreferences。只能通過sqlite數據庫實現。

解决方案 »

  1.   

    对于“程序”指的是什么我不太明白。我可否理解你上面那句话为SharedPreferences只能作用于一个activity内(譬如A extends activity),其他的activity是无法使用SharedPreferences当中的数据的(譬如B extends activity ,B当中无法对A中存储在SharedPreferences中的数据进行操作)?
      

  2.   

    SharedPreferences settings = getSharedPreferences(SETTING_Infos, 0); 
    mBootNumEdit.setText(String.valueOf(mBootNum));
    mSleepTimeEdit.setText(String.valueOf(mSleepTime));
    settings.edit()
    .putString(BootNum, mBootNumEdit.getText().toString()) 
    .putString(SleepTime, mSleepTimeEdit.getText().toString()) 
    .commit(); 

    Log.i("mBootNumEdit",String.valueOf(mBootNum));
    Log.i("mBootNumEdit",String.valueOf(mSleepTime)); 
    存的是String
    ====================================================================
    取得是int
    private void GetSharedPreferences() {
    BootNumTV=(TextView)findViewById(R.id.Bootnum);
    SleepTimeTV=(TextView)findViewById(R.id.Sleeptime);
            SharedPreferences settings = getSharedPreferences(SETTING_Infos, 0);
            mBootNum=settings.getInt(BootNum, 1);
            mSleepTime=settings.getInt("SleepTime", 1); 
            BootNumTV.setText(String.valueOf(mBootNum));
            SleepTimeTV.setText(String.valueOf(mBootNum));

    =====================
    异常信息
    E/AndroidRuntime( 1558): java.lang.RuntimeException: Unable to start activity ComponentInfo{wistron.mojo/wistron.mojo.boot.UpdateProgress}: java.lang.ClassCastException: java.lang.String
    ================================
    至于SharedPreferences所存的值在当前应用的任何程序都可以调用只要是本应用
      

  3.   

    一个application中的所有activity可以共享。不同application不可以。
      

  4.   

    如果SharedPreferences只能作用于一个activity内,假如我现在想在2个activity中实现类似注册功能改如何操作,可以调用哪些API实现?谢谢!
    A extends Activity( A填写用户名,密码)
    B extends Activity( B显示A中提交的用户名,密码)
      

  5.   

    看的晕,你即说如果SharedPreferences只能作用于一个activity内 又说想在2个activity中实现类似注册功能改如何操作
    没有理解什么意思Bundle类可以在intent页面跳转的时候传递数据,作用范围和数据范围:传送方、接收方  当传送方和接受方被finish()数据随之消失
      

  6.   

    谢谢,上面2为朋友的回答最初没看见,所以理解错了才有上一帖的提问。
     
    在Android SDK2.0中关于SharedPreferences有这么一句话:Assign a name to your set of preferences if you want to share them with other components in the same application, 
     
    在Google I_O 的文档Inside_the_Android_Application_Framework.pdf中有关于Process,Activity,application之间 的关系图
     
    我再试试。请问下,可以在哪里上传图片?
      

  7.   

    好好检查你的代码,是一个java.lang.ClassCastException: java.lang.String 错误;另:只要是一个app中,就可以共享SP
      

  8.   

    另外补充一下,SP也可以在不同应用之间共享,权限可以自己指定!见下面的modepublic abstract SharedPreferences getSharedPreferences (String name, int mode)Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.
    Parameters
    name  Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()).
    mode  Operating mode. Use 0 or MODE_PRIVATE for the default operation, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions.
    Returns    * Returns the single SharedPreferences instance that can be used to retrieve and modify the preference values.See Also    * MODE_PRIVATE
        * MODE_WORLD_READABLE
        * MODE_WORLD_WRITEABLE
      

  9.   

    RebootTest   存的是String 
    ==================================================================== 
    UpdateProgress  取得是int 问题已经解决,正如dongsheng_hu指出的一样,所以改用
    settings.edit()
    .putInt(BootNum,Integer.valueOf(mBootNumEdit.getText().toString()))
    .putInt(SleepTime,Integer.valueOf(mSleepTimeEdit.getText().toString()))
    .commit();
    就OK了。超级汗,由于.xml文件中定义了太多的view id。结果不小心引用错了id,找了半天才找到nullpointexception异常出现的原因。谢谢大家。