voidonCreate(BundlesavedInstanceState){...// Get intent, action and MIME type Intentintent=getIntent();Stringaction=intent.getAction();Stringtype=intent.getType();if(Intent.ACTION_SEND.equals(action)&&type!=null){if("text/plain".equals(type)){handleSendText(intent);// Handle text being sent }elseif(type.startsWith("image/")){handleSendImage(intent);// Handle single image being sent }}elseif(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(Intentintent){StringsharedText=intent.getStringExtra(Intent.EXTRA_TEXT);if(sharedText!=null){// Update UI to reflect text being shared }}voidhandleSendImage(Intentintent){UriimageUri=(Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);if(imageUri!=null){// Update UI to reflect image being shared }}voidhandleSendMultipleImages(Intentintent){ArrayList<Uri>imageUris=intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);if(imageUris!=null){// Update UI to reflect multiple images being shared }}
请注意,因为你无法知道其他程序发送过来的数据内容是文本还是其他的数据,因此你需要避免在UI线程里面去处理那些获取到的数据。
更新UI可以像更新EditText一样简单,也可以是更加复杂一点的操作,例如过滤出感兴趣的图片。It’s really specific to your application what happens next.