标签:
保存到SharePreference
1.获取SharePreference
getSharePreferences();名称参数通过第一个参数来指定,可在app中任何一个Context执行该方法
getPreferences();不需要提供文件名称
例如在一个Fragment中执行的示例:
Context context = getActivity(); SharedPreferences sharedPref = context.getSharedPreferences( getString(R.string.preference_file_key), Context.MODE_PRIVATE);
当activity仅需要一个shared preferences文件时,可以使用getPreferences()方法
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
注意:private模式仅能被我们的app访问
如果创建了一个MODE_WORLD_READABLE或者MODE_WORLD_WRITEABLE 模式的shared preference文件,
则其他任何app均可通过文件名访问该文件。
2.通过执行edit()创建一个 SharedPreferences.Editor
SharedPreferences.Editor editor = sharedPref.edit();
3.通过类似putInt()与putString()等方法传递keys与values
editor.putInt(getString(R.string.saved_high_score), newHighScore);
4.最后通过commit() 提交改变
editor.commit();
更新manifest文件
<activity android:name=".ui.MyActivity"> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="image/*" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND_MULTIPLE" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="image/*" /> </intent-filter> </activity>
处理接受到的数据
void onCreate(Bundle savedInstanceState) { ... // Get intent, action and MIME type Intent intent = getIntent(); String action = intent.getAction(); String type = intent.getType(); if (Intent.ACTION_SEND.equals(action) && type != null) { if ("text/plain".equals(type)) { handleSendText(intent); // Handle text being sent } else if (type.startsWith("image/")) { handleSendImage(intent); // Handle single image being sent } } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) { if (type.startsWith("image/")) { handleSendMultipleImages(intent); // Handle multiple images being sent } } else { // Handle other intents, such as being started from the home screen } ... } void handleSendText(Intent intent) { String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); if (sharedText != null) { // Update UI to reflect text being shared } } void handleSendImage(Intent intent) { Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); if (imageUri != null) { // Update UI to reflect image being shared } } void handleSendMultipleImages(Intent intent) { ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); if (imageUris != null) { // Update UI to reflect multiple images being shared } }
标签:
原文地址:http://www.cnblogs.com/cxsy/p/5664706.html