欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android應(yīng)用中使用及實(shí)現(xiàn)系統(tǒng)“分享”接口實(shí)例

 更新時(shí)間:2016年12月17日 10:57:27   作者:xyz_lmn  
為了應(yīng)用的推廣、傳播,很多的應(yīng)用中都有“分享”功能,這篇文章主要介紹了Android應(yīng)用中使用及實(shí)現(xiàn)系統(tǒng)“分享”接口實(shí)例,有興趣的可以了解一下。

為了應(yīng)用的推廣、傳播,很多的應(yīng)用中都有“分享”功能,一個(gè)按鈕,點(diǎn)擊后會(huì)出現(xiàn)短信、微博等等一切實(shí)現(xiàn)了分享功能的應(yīng)用列表。這一篇文章主要介紹怎么調(diào)用分享功能和怎么實(shí)現(xiàn)分享接口讓自己應(yīng)用出現(xiàn)分享列表中。Android應(yīng)用中能很方便的完成這些功能,這也正是Android的偉大之處,他能很簡(jiǎn)單的完成應(yīng)用之間的溝通以相互整合。

調(diào)用分享功能

1、分享文本

分享功能使用的隱式啟動(dòng)Activity的方法,這里的Action使用的是 ACTION_SEND。

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

效果如下圖的圖一。

2、改變分享列表標(biāo)題

使用上面的分享方式分享列表標(biāo)題為“使用一下內(nèi)容完成操作”,Android中提供了Intent.createChooser() , 這樣能一直顯示分享選擇列表,并且修改了分享列表標(biāo)題內(nèi)容。

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

使用Intent.createChooser()的好處:

If you callIntent.createChooser() for the intent, Android will always display the chooser. This has some advantages:

  • Even if the user has previously selected a default action for this intent, the chooser will still be displayed.
  • If no applications match, Android displays a system message.
  • You can specify a title for the chooser dialog.

           

分享功能不只是Intent.EXTRA_TEXT,還可以 EXTRA_EMAIL ,EXTRA_CC , EXTRA_BCC  ,EXTRA_SUBJECT . 只需要接受方完成響應(yīng)數(shù)據(jù)接受。

3、分享圖片

分享功能還支持二進(jìn)制內(nèi)容(Binary Content),但是多數(shù)是處理的圖片,因?yàn)閟hareIntent.setType("image/jpeg")這一項(xiàng)設(shè)置了內(nèi)容類型??梢惨允瞧渌愋?,需要接受方支持。

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

4、分享圖片列表

分享功能不僅支持單張圖片,還支持圖片列表,這里還是說的范圍太窄了,應(yīng)該聲明不僅僅是圖片。

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

實(shí)現(xiàn)分享功能

上面說的都是怎么調(diào)用分享功能,以下就開始寫怎么實(shí)現(xiàn)分享功能,讓我們的應(yīng)用也出現(xiàn)在分享列表中。前面也說了分享功能是使用隱式調(diào)用Activtiy實(shí)現(xiàn)的,Activity需要聲明 <intent-filter> 。

聲明intent-filter

<activity 
      android:name="com.example.sharedemo.ShareActivity" 
      android:label="@string/app_name" > 
      <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> 

上面聲明了三種intent-filter,當(dāng)然可以更多,這里只是舉個(gè)例子,

處理接收數(shù)據(jù)
聲明了intent-filter,響應(yīng)的Activity就要處理響應(yīng)的數(shù)據(jù),示例如下:

public class ShareActivity extends Activity{ 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(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); 
    String sharedTitle = intent.getStringExtra(Intent.EXTRA_TITLE); 
    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 
    } 
  } 
} 

通過聲明intent-filter,處理接受到的數(shù)據(jù)就能完成分享的接收功能。

更多

上面只做了分享功能簡(jiǎn)單的說明,伴隨著Android api的升級(jí),也出現(xiàn)了一些新的完成“分享”功能的方法,比如 ShareActionProvider ,更多請(qǐng)參考

demo下載:demo

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論