android-share simple data
2015-12-30 11:10
483 查看
Sending and receiving data between applications with intents is most commonly used for social sharing of content. Intents allow users to share information quickly and easily, using
their favorite applications.
Note: The best
way to add a share action item to an
to use
which became available in API level 14.
discussed in the lesson about Adding an Easy
Share Action.
> send text content
> send binary content
>Send Multiple Pieces of Content
> if your application handles receiving text content, a single image of any type, or multiple images of any type, your manifest would look like:
> Keep in mind that if this activity can be started from other parts of the system, such as the launcher, then you will need to take this into consideration when examining the
intent.
Updating the UI can be as simple as populating an
or it can be more complicated like applying an interesting photo filter to an image. It's really specific to your application what happens next.
Note:
available starting with API Level 14 and higher.
You may only need to set the share intent once during the creation of your menus, or you may want to set it and then update it as the UI changes. For example, when you view photos full screen in the Gallery app, the sharing intent changes as you flip between
photos.
their favorite applications.
Note: The best
way to add a share action item to an
ActionBaris
to use
ShareActionProvider,
which became available in API level 14.
ShareActionProvideris
discussed in the lesson about Adding an Easy
Share Action.
> send text content
Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); sendIntent.setType("text/plain"); startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
> send binary content
Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage); shareIntent.setType("image/jpeg"); startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
>Send Multiple Pieces of Content
ArrayList<Uri> imageUris = new ArrayList<Uri>(); imageUris.add(imageUri1); // Add your image URIs here imageUris.add(imageUri2); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris); shareIntent.setType("image/*"); startActivity(Intent.createChooser(shareIntent, "Share images to.."));
> if your application handles receiving text content, a single image of any type, or multiple images of any type, your manifest would look like:
<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>
> Keep in mind that if this activity can be started from other parts of the system, such as the launcher, then you will need to take this into consideration when examining the
intent.
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 } }
Updating the UI can be as simple as populating an
EditText,
or it can be more complicated like applying an interesting photo filter to an image. It's really specific to your application what happens next.
Note:
ShareActionProvideris
available starting with API Level 14 and higher.
private ShareActionProvider mShareActionProvider; ... @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate menu resource file. getMenuInflater().inflate(R.menu.share_menu, menu); // Locate MenuItem with ShareActionProvider MenuItem item = menu.findItem(R.id.menu_item_share); // Fetch and store ShareActionProvider mShareActionProvider = (ShareActionProvider) item.getActionProvider(); // Return true to display menu return true; } // Call to update the share intent private void setShareIntent(Intent shareIntent) { if (mShareActionProvider != null) { mShareActionProvider.setShareIntent(shareIntent); } }
You may only need to set the share intent once during the creation of your menus, or you may want to set it and then update it as the UI changes. For example, when you view photos full screen in the Gallery app, the sharing intent changes as you flip between
photos.
相关文章推荐
- GreenDao的简单使用说明(一)android studio中引入GreenDao
- GreenDao的简单使用说明(一)android studio中引入GreenDao
- android 6.0编译错误
- Android OOM问题排查
- Android TextureView简易教程
- Android Studio代码自动提示无效(not available in Power Save mode)
- android垃圾回收机制及程序优化System.gc
- Android Studio 中 Gradle 基础
- Android开发总结笔记 绘图工具类 3-6
- aandroid 开发常见bug
- Android高仿网易新闻客户端之首页
- Android 广播大全 Intent Action 事件
- genymotion模拟器结合Android studio使用
- Android框架 - Volley
- Android开源项目 Universal imageloader 源码研究之项目框架
- Android进程与内存及内存泄露
- Android DrawerLayout 高仿QQ5.2双向侧滑菜单
- Chrome模拟手机浏览器(iOS/Android)的三种方法,亲测无误!
- AndroidStudio快捷键
- Android 选取手机相册图片方法