2012-08-14 41 views
1

我有一個應用程序(com.example.MyApplication)的sharedPreferences節省了當前的位置這樣檢索另一個應用程序

SharedPreferences settings = getSharedPreferences(PREFERENCES, 0); 
       SharedPreferences.Editor editor = settings.edit(); 
       editor.putString("City", city); 
       editor.commit(); 

現在我想使其它應用程序(com.example.SecondApplication)和檢索來自其他應用程序的城市字符串,我該怎麼做?

+0

如果MyApplication不存在,SecondApplication會做什麼?或存在一段時間,然後卸載? – CommonsWare 2012-08-14 19:22:50

+0

我認爲共享首選項只能在同一應用程序中訪問,而不能在任何其他應用程序中訪問。 – 2012-08-14 19:25:46

+0

當我試圖城市我會做一個if語句,如果city == null,那麼我會顯示一條消息,說城市沒有保存 – DoubleP90 2012-08-14 19:26:54

回答

1

最後我找到了一種方法:d 我救了我的喜好這樣

SharedPreferences settings = getSharedPreferences(PREFERENCES, MODE_PRIVATE); 
       SharedPreferences.Editor editor = settings.edit(); 
       editor.putString("City", city); 
       editor.commit(); 

我取回他們這樣

Context otherAppsContext = null; 
      try { 
       otherAppsContext = createPackageContext("com.example.FirstApp", 0); 
      } catch (NameNotFoundException e) { 
      } 



      SharedPreferences settings = otherAppsContext.getSharedPreferences(PREFERENCES, Context.CONTEXT_INCLUDE_CODE); 
      text.setText(settings.getString("City", "nope")); 

而且重要的是,清單中的我把已這兩個應用程序

android:sharedUserId="example.shared" 

我已經把它放在android:versionName

相關問題