您的位置:首页 > 移动开发 > Android开发

Android开发官方文档---Sharing Simple Data

2015-12-22 15:29 483 查看
发送简单数据到其他应用

发送文本数据

Intent sendIntent = newIntent(); 

sendIntent.setAction(Intent.ACTION_SEND); 

sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text tosend."); 

sendIntent.setType("text/plain"); 

startActivity(Intent.createChooser(sendIntent,getResources().getText(R.string.send_to)));

 


 

发送二进制内容

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)));

 

发送多数据

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.."));

 

接收简单数据

设置<intent-filter>标签指定接收处理的动作和数据类型

<activityandroid: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 (BundlesavedInstanceState) { 

    ... 

    // Get intent, action and MIMEtype 

    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); // Handletext being sent 

        } elseif (type.startsWith("image/")) { 

            handleSendImage(intent); // Handlesingle 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 

    } 

    ... 



 

voidhandleSendText(Intent intent) { 

    String sharedText =intent.getStringExtra(Intent.EXTRA_TEXT); 

    if (sharedText != null) { 

        //Update UI to reflect text being shared 

    } 



 

voidhandleSendImage(Intent intent) { 

    Uri imageUri = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM); 

    if (imageUri != null) { 

        //Update UI to reflect image being shared 

    } 



 

voidhandleSendMultipleImages(Intent intent) { 

    ArrayList<Uri> imageUris =intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); 

    if (imageUris != null) { 

        //Update UI to reflect multiple images being shared 

    } 

}

 

动作栏的share操作

注意:使用 ShareActionProvider  要 API 14 以上

修改菜单栏的XML文件,设置actionProviderClass属性

<menuxmlns:android="http://schemas.android.com/apk/res/android"> 

    <item 

            android:id="@+id/menu_item_share" 

            android:showAsAction="ifRoom" 

            android:title="Share" 

            android:actionProviderClass= 

               "android.widget.ShareActionProvider"/> 

    ... 

</menu>

 


设置share的Intent

privateShareActionProvider mShareActionProvider; 

... 

 

@Override 

public booleanonCreateOptionsMenu(Menu menu) { 

    // Inflate menu resource file. 

   getMenuInflater().inflate(R.menu.share_menu, menu); 

 

    // Locate MenuItem withShareActionProvider 

    MenuItem item =menu.findItem(R.id.menu_item_share); 

 

    // Fetch and storeShareActionProvider 

   mShareActionProvider = (ShareActionProvider) item.getActionProvider(); 

 

    // Return true to display menu 

    return true; 



 

// Call to update theshare intent 

private voidsetShareIntent(Intent shareIntent) { 

    if (mShareActionProvider != null){ 

        mShareActionProvider.setShareIntent(shareIntent); 

    } 

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android share data