2016-09-30 8 views
0

私は2つのアプリを "AppOne"と "AppTwo"を想定しています.AppOneでは、 "appTwo"からこの値を取得したいのですが、 "String" name "のような共有のPrefrenceにいくつかの値が格納されています。 AppOne Sahredの好みのものアンドロイドの他のアプリからの他のアプリの共有設定から値を取得するにはどうすればよいですか?

コード: -

private SharedPreferences m_Preference; 
private SharedPreferences.Editor m_Editor; 
private final String MY_PREF="AppData"; 

public PreferenceHelper(Context context){ 
    this.m_Preference = context.getSharedPreferences(MY_PREF,Context.MODE_PRIVATE); 
    this.m_Editor = m_Preference.edit(); 
} 
/*Saving String value......*/ 
public void saveStringPreference(String key,String value){ 
    m_Editor.putString(key,value); 
    m_Editor.apply(); 
} 
public String getStringPreference(String key){ 
    return m_Preference.getString(key,""); 
} 

/*Saving int value........*/ 
public void saveIntegerValue(String key,int value){ 
    m_Editor.putInt(key,value); 
    m_Editor.apply(); 
} 
public int getIntPreference(String key){ 
    return m_Preference.getInt(key,1); 
} 

そしてMainActivityで、私はその値を保存します -

preferenceHelper =新しいPreferenceHelper(getApplicationContext());

preferenceHelper.saveStringPreference("Name", "ABC"); 
+0

::など – CommonsWare

+0

'ContentProvider'、リモート' Service'、ファイルを読み込むためのtxtファイルを書き込むには

public void writeToFile(String data) { // Get the directory for the user's public pictures directory. final File path = Environment.getExternalStoragePublicDirectory ( //Environment.DIRECTORY_PICTURES Environment.DIRECTORY_DCIM + "/YourFolder/" ); // Make sure the path directory exists. if(!path.exists()) { // Make it, if it doesn't exit path.mkdirs(); } final File file = new File(path, "myText.txt"); // Save your stream, don't forget to flush() it before closing it. try { file.createNewFile(); FileOutputStream fOut = new FileOutputStream(file); OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); myOutWriter.append(data); myOutWriter.close(); fOut.flush(); fOut.close(); } catch (IOException e) { Log.e("Exception", "File write failed: " + e.toString()); } } 

MODE_PRIVATEは、データがapにプライベートであることを意味します。このリンクをチェックするhttp://stackoverflow.com/a/6030399/4818247 –

+0

Context.MODE_PRIVATEを使用しているので、他のアプリからこの環境設定にアクセスするべきではありません。 –

答えて

0

あなたはそれをすることはできません。 SharedPreferencesは、アプリケーションのパッケージにローカルであり、外部から直接アクセスすることはできません(セキュリティ上の理由、おそらく)。

あなたは(SharedPrefsまたはその他のデータ)他のアプリがあなたのアプリからいくつかのデータを取得することができるようにしたい場合は、ContentProviderを定義する必要があります、

:-) BroadcastReceiverServiceまたは外部(ウェブ)API
0

sharedPreferencesを使用してデータを保存する代わりに、ファイルを書き込まないのはなぜですか。 この方法で、別のアンドロイドアプリケーションからデータにアクセスできます。アプリの2つは使用しているアプリケーションの一つで、いくつかのAPIを実装し

public String readTheFile(){ 
//*Don't* hardcode "/sdcard" 
File sdcard = Environment.getExternalStorageDirectory(); 

//Get the text file 
File file = new File(sdcard,"myText.txt"); 

//Read text from file 
StringBuilder text = new StringBuilder(); 

try { 
    BufferedReader br = new BufferedReader(new FileReader(file)); 
    String line; 

    while ((line = br.readLine()) != null) { 
     text.append(line); 
     text.append('\n'); 
    } 
    br.close(); 
} 
catch (IOException e) { 
    //You'll need to add proper error handling here 
} 
return text.toString(); 
} 
+0

これは素晴らしい解決策ですが、OPはこれを行う方法を探していて、他のアプリのデータにアクセスするには – Kelevandos

+0

@Kelevandos、それはAndroidにどのような種類のデータを保存すべきかを示します。私は、小さな文字列などを保存するためにファイルを共有したり書き込んだりするのが好きです。この場合、contentProviderを使用するよりも、ファイルの印刷や読み込みがはるかに簡単です。 – ziLk

+0

はい、正しいですが、通信したい両方のアプリの開発者である場合にのみ – Kelevandos

関連する問題