Android中實現APP常值內容的分享發送與接收方法簡述

來源:互聯網
上載者:User

標籤:tar   問題   額外   back   資訊   protect   cti   content   default   

謹記(指定選取器Intent.createChooser())

開始今天的內容前,先閑聊一下:

(1)突然有一天頭腦風暴,對很多問題有了新的看法和見解,迫不及待的想要分享給大家,文檔已經寫好了,我需要通過或者QQ,簡訊等社交工具發送給大家。

(2)在網上發現一段特別好的文章,想要儲存收藏下來。

上面描述了進入智能手機時代後,我們經常遇到的兩種情況,那麼作為開發人員的我們如何讓自己開發的APP實現這兩種功能呢,下面我們以實現文本的發送分享以及接收來梳理下兩種功能的實現過程(其他類型的資料在博文末尾會給大家做簡單介紹)。

第一種情況:APP實現發送分享文本功能

在實現APP發送與分享的功能時,根據是否指定選取器Intent.createChooser(),會有兩種不同的實現效果。

(1)指定選取器的實現效果如下:

每次需要發送分享的時候,都會彈出所有具有分享功能的APP供選擇。(個人認為很人性化)

(2)未指定選取器的實現效果如下:

圖中示範測試使用的安卓原生系統,在未設定選取器的時候,每次會提醒使用者使用當前APP提交發送分享所使用的APP僅使用一次還是始終都使用(經測試萬一大家手滑,誤點了“始終”,那麼好吧,如果下次想換其他APP分享內容時,除非你卸載重裝當前APP);但在其他一些安卓定製系統的品牌手機上測試時,發現僅第一次會跳出所有具有發送分享功能的APP供你選擇(但是不會提示你僅使用一次還是始終),一旦選擇後,後果與在原生系統上點擊始終的效果相同。立馬卸載APP的心都有了。

好了,實現效果大家都看到了,我們開始擼一把代碼吧:

第一步:Layout中介面布局檔案activity_main.xml檔案(文本編輯框以及按鈕):
 1 <?xml version="1.0" encoding="utf-8"?> 2     <LinearLayout 3     xmlns:android="http://schemas.android.com/apk/res/android" 4     xmlns:tools="http://schemas.android.com/tools" 5     android:id="@+id/activity_main" 6     android:layout_width="match_parent" 7     android:layout_height="match_parent" 8     android:orientation="vertical" 9     tools:context="com.example.administrator.sendshare.MainActivity">10     <EditText11         android:id="@+id/et"12         android:layout_width="match_parent"13         android:layout_height="wrap_content"14         android:hint="想給潘侯爺說點什麼"/>15     <Button16         android:layout_width="match_parent"17         android:layout_height="wrap_content"18         android:onClick="sendtext"19         android:text="發送文本" />20 </LinearLayout>
第二步:Java中實現代碼MainActivity.java功能實現代碼:

注意注意:指定選取器啊

 1 public class MainActivity extends AppCompatActivity { 2     EditText et;//聲明文本編輯框 3     String str;//聲明字串,用於擷取文本編輯框內的內容 4     @Override 5     protected void onCreate(Bundle savedInstanceState) { 6         super.onCreate(savedInstanceState); 7         setContentView(R.layout.activity_main); 8         //擷取文字框 9         et = (EditText) findViewById(R.id.et);10     }11     //建立方法將輸入的內容發出去12     public void sendtext(View view){13         str=et.getText().toString();14         Intent intent = new Intent();15         /*設定action為發送分享,16         *並判斷要發送分享的內容是否為空白17          */18         intent.setAction(Intent.ACTION_SEND);19         if(str!=null){20             intent.putExtra(Intent.EXTRA_TEXT,str);21         }else{22             intent.putExtra(Intent.EXTRA_TEXT,"");23         }24         intent.setType("text/plain");//設定分享發送的資料類型25         //未指定選取器,部分定製系統首次選擇後,後期將無法再次改變26 //        startActivity(intent);27         //指定選取器選擇使用有發送文本功能的App28         startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name)));29     }30 }
第二種情況:APP實現接收分享文本功能

實現效果如下(將簡訊內容分享到我們的APP上):

layout布局介面為初始預設,僅一個預設helloworld的TextView介面,這裡就省略不寫了。

第一步:AndroidMainfest.xml設定檔(添加接收文本所需的action等intent屬性)
 1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3     package="com.example.administrator.test" > 4     <application 5         android:allowBackup="true" 6         android:icon="@mipmap/ic_launcher" 7         android:label="@string/app_name" 8         android:supportsRtl="true" 9         android:theme="@style/AppTheme" >10         <activity android:name=".MainActivity" >11             <intent-filter>12                 <action android:name="android.intent.action.MAIN" />13                 //添加接收文本用的action,category,mimeType14                 <action android:name="android.intent.action.SEND" />15                 <category android:name="android.intent.category.DEFAULT" />16                 <data android:mimeType="text/plain" />17                 <category android:name="android.intent.category.LAUNCHER" />18             </intent-filter>19         </activity>20     </application>21 </manifest>
第二步:Java中實現代碼MainActivity.java功能實現代碼
 1 import android.content.Intent; 2 import android.support.v7.app.AppCompatActivity; 3 import android.os.Bundle; 4 import android.widget.TextView; 5 public class MainActivity extends AppCompatActivity { 6     private TextView tv; 7     @Override 8     protected void onCreate(Bundle savedInstanceState) { 9         super.onCreate(savedInstanceState);10         setContentView(R.layout.activity_main);11         tv = (TextView) findViewById(R.id.tv);12         //擷取intent13         Intent intent =getIntent();14         String action = intent.getAction();15         String type = intent.getType();16         //設定接收類型為文本17         if (Intent.ACTION_SEND.equals(action) && type != null){18             if ("text/plain".equals(type)) {19                 handlerText(intent);20             }21         }22 }23 //該方法用於擷取intent所包含的文本資訊,並顯示到APP的Activity介面上24     private void handlerText(Intent intent) {25         String data = intent.getStringExtra(Intent.EXTRA_TEXT);26         tv.setText(data);27     }28 }
額外補充:設定更新案頭背景,核心代碼如下:
 1 import android.content.Intent; 2 import android.support.v7.app.AppCompatActivity; 3 import android.os.Bundle; 4 import android.view.View; 5 public class Main2Activity extends AppCompatActivity { 6     @Override 7     protected void onCreate(Bundle savedInstanceState) { 8         super.onCreate(savedInstanceState); 9         setContentView(R.layout.activity_main2);10     }11     public void select(View view){12         Intent intent = new Intent();13         intent.setAction(Intent.ACTION_SET_WALLPAPER);14         startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name)));15 //        startActivity(intent);16     }17 }
以二進位的形式分享發送圖片,核心代碼如下:
1 public void sendimage(View view) {2     Intent intent = new Intent();3     intent.setAction(Intent.ACTION_SEND);4     intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory()+"/DCIM/Camera/20161204_195228.jpg"));5     intent.setType("image/*");6 //  startActivity(intent);7     startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name)));8 }
發送分享多張圖片,核心代碼如下:
 1 public void sendimages(View view) { 2     ArrayList<Uri> uris = new ArrayList<>(); 3     //示範發送兩張圖片 4     uris.add(Uri.parse(Environment.getExternalStorageDirectory()+"/DCIM/Camera/20161204_195228.jpg")); 5     uris.add(Uri.parse(Environment.getExternalStorageDirectory()+"/DCIM/Camera/20161204_195155.jpg")); 6     Intent intent = new Intent(); 7     intent.setAction(Intent.ACTION_SEND_MULTIPLE); 8     intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uris); 9     intent.setType("image/*");10 //        startActivity(intent);11     startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name)));12 }


今天到這了,有問題歡迎評論討論,晚安嘍!

Android中實現APP常值內容的分享發送與接收方法簡述

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.