Skip to content

Instantly share code, notes, and snippets.

@ryokosuge
Last active August 29, 2015 14:05
Show Gist options
  • Save ryokosuge/f8574e2233df13d234b0 to your computer and use it in GitHub Desktop.
Save ryokosuge/f8574e2233df13d234b0 to your computer and use it in GitHub Desktop.
【Android】Androidのアプリ間で情報を共有する方法 rel : http://blog.ryochin.xyz/archives/41
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
// SharedPreferencesの生成
SharedPreferences sp = this.getSharedPreferences("MainActivity", Context.MODE_PRIVATE);
// Editorの生成
Editor editor = sp.edit();
// 文字列の保存
editor.putString("KEY", "VALUE");
editor.commit();
// 保存した文字列の参照
String value = sp.getString("KEY", "");
// 第2引数がなかった場合のdefaultの値になるので、とれたか確認
if (value.equals("")){
Log.d("MainActivity", "値がとれませんでした");
} else {
Log.d("MainActivity", "値がとれました");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment