簡(jiǎn)述Android中實(shí)現(xiàn)APP文本內(nèi)容的分享發(fā)送與接收方法
謹(jǐn)記(指定選擇器Intent.createChooser())
開(kāi)始今天的內(nèi)容前,先閑聊一下:
(1)突然有一天頭腦風(fēng)暴,對(duì)很多問(wèn)題有了新的看法和見(jiàn)解,迫不及待的想要分享給大家,文檔已經(jīng)寫(xiě)好了,我需要通過(guò)微信或者QQ,短信等社交工具發(fā)送給大家。
(2)在網(wǎng)上發(fā)現(xiàn)一段特別好的文章,想要保存收藏下來(lái)。
上面描述了進(jìn)入智能手機(jī)時(shí)代后,我們經(jīng)常遇到的兩種情況,那么作為開(kāi)發(fā)者的我們?nèi)绾巫屪约洪_(kāi)發(fā)的APP實(shí)現(xiàn)這兩種功能呢,下面我們以實(shí)現(xiàn)文本的發(fā)送分享以及接收來(lái)梳理下兩種功能的實(shí)現(xiàn)過(guò)程(其他類型的數(shù)據(jù)在博文末尾會(huì)給大家做簡(jiǎn)單介紹)。
第一種情況:APP實(shí)現(xiàn)發(fā)送分享文本功能
在實(shí)現(xiàn)APP發(fā)送與分享的功能時(shí),根據(jù)是否指定選擇器Intent.createChooser(),會(huì)有兩種不同的實(shí)現(xiàn)效果。
(1)指定選擇器的實(shí)現(xiàn)效果如下:
每次需要發(fā)送分享的時(shí)候,都會(huì)彈出所有具有分享功能的APP供選擇。(個(gè)人認(rèn)為很人性化)
(2)未指定選擇器的實(shí)現(xiàn)效果如下:
圖中演示測(cè)試使用的安卓原生系統(tǒng),在未設(shè)置選擇器的時(shí)候,每次會(huì)提醒用戶使用當(dāng)前APP提交發(fā)送分享所使用的APP僅使用一次還是始終都使用(經(jīng)測(cè)試萬(wàn)一大家手滑,誤點(diǎn)了“始終”,那么好吧,如果下次想換其他APP分享內(nèi)容時(shí),除非你卸載重裝當(dāng)前APP);但在其他一些安卓定制系統(tǒng)的品牌手機(jī)上測(cè)試時(shí),發(fā)現(xiàn)僅第一次會(huì)跳出所有具有發(fā)送分享功能的APP供你選擇(但是不會(huì)提示你僅使用一次還是始終),一旦選擇后,后果與在原生系統(tǒng)上點(diǎn)擊始終的效果相同。立馬卸載APP的心都有了。
好了,實(shí)現(xiàn)效果大家都看到了,我們開(kāi)始擼一把代碼吧:
第一步:Layout中界面布局文件activity_main.xml文件(文本編輯框以及按鈕):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.administrator.sendshare.MainActivity"> <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="想給潘侯爺說(shuō)點(diǎn)什么"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="sendtext" android:text="發(fā)送文本" /> </LinearLayout>
第二步:Java中實(shí)現(xiàn)代碼MainActivity.java功能實(shí)現(xiàn)代碼:
注意注意:指定選擇器啊
public class MainActivity extends AppCompatActivity { EditText et;//聲明文本編輯框 String str;//聲明字符串,用于獲取文本編輯框內(nèi)的內(nèi)容 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取文本框 et = (EditText) findViewById(R.id.et); } //創(chuàng)建方法將輸入的內(nèi)容發(fā)出去 public void sendtext(View view){ str=et.getText().toString(); Intent intent = new Intent(); /*設(shè)置action為發(fā)送分享, *并判斷要發(fā)送分享的內(nèi)容是否為空 */ intent.setAction(Intent.ACTION_SEND); if(str!=null){ intent.putExtra(Intent.EXTRA_TEXT,str); }else{ intent.putExtra(Intent.EXTRA_TEXT,""); } intent.setType("text/plain");//設(shè)置分享發(fā)送的數(shù)據(jù)類型 //未指定選擇器,部分定制系統(tǒng)首次選擇后,后期將無(wú)法再次改變 // startActivity(intent); //指定選擇器選擇使用有發(fā)送文本功能的App startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name))); } }
第二種情況:APP實(shí)現(xiàn)接收分享文本功能
實(shí)現(xiàn)效果如下(將短信內(nèi)容分享到我們的APP上):
layout布局界面為初始默認(rèn),僅一個(gè)默認(rèn)helloworld的TextView界面,這里就省略不寫(xiě)了。
第一步:AndroidMainfest.xml配置文件(添加接收文本所需的action等intent屬性)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.administrator.test" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> //添加接收文本用的action,category,mimeType <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
第二步:Java中實(shí)現(xiàn)代碼MainActivity.java功能實(shí)現(xiàn)代碼
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); //獲取intent Intent intent =getIntent(); String action = intent.getAction(); String type = intent.getType(); //設(shè)置接收類型為文本 if (Intent.ACTION_SEND.equals(action) && type != null){ if ("text/plain".equals(type)) { handlerText(intent); } } } //該方法用于獲取intent所包含的文本信息,并顯示到APP的Activity界面上 private void handlerText(Intent intent) { String data = intent.getStringExtra(Intent.EXTRA_TEXT); tv.setText(data); } }
額外補(bǔ)充:
設(shè)置更新桌面背景,核心代碼如下:
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); } public void select(View view){ Intent intent = new Intent(); intent.setAction(Intent.ACTION_SET_WALLPAPER); startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name))); // startActivity(intent); } }
以二進(jìn)制的形式分享發(fā)送圖片,核心代碼如下:
public void sendimage(View view) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory()+"/DCIM/Camera/20161204_195228.jpg")); intent.setType("image/*"); // startActivity(intent); startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name))); }
發(fā)送分享多張圖片,核心代碼如下:
public void sendimages(View view) { ArrayList<Uri> uris = new ArrayList<>(); //演示發(fā)送兩張圖片 uris.add(Uri.parse(Environment.getExternalStorageDirectory()+"/DCIM/Camera/20161204_195228.jpg")); uris.add(Uri.parse(Environment.getExternalStorageDirectory()+"/DCIM/Camera/20161204_195155.jpg")); Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND_MULTIPLE); intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uris); intent.setType("image/*"); // startActivity(intent); startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name))); }
今天到這了,有問(wèn)題歡迎評(píng)論討論,晚安嘍!
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
Android中webView加載H5綁定cookie實(shí)例
這篇文章主要介紹了Android中webView加載H5綁定cookie實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android實(shí)現(xiàn)開(kāi)機(jī)自動(dòng)啟動(dòng)Service或app的方法
這篇文章主要介紹了Android實(shí)現(xiàn)開(kāi)機(jī)自動(dòng)啟動(dòng)Service或app的方法,結(jié)合實(shí)例形式分析了Android開(kāi)機(jī)自啟動(dòng)程序的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07Android通過(guò)記住密碼功能學(xué)習(xí)數(shù)據(jù)存儲(chǔ)類SharedPreferences詳解及實(shí)例
這篇文章主要通過(guò)“記住密碼”實(shí)例功能學(xué)習(xí)為大家介紹了Android數(shù)據(jù)存儲(chǔ)類SharedPreferences,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03Andriod事件分發(fā)事件由來(lái)初識(shí)
這篇文章主要為大家講解了Andriod事件分發(fā)事件由來(lái)的初步認(rèn)識(shí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Android 中使用ContentObserver模式獲取短信用正則自動(dòng)填充驗(yàn)證碼
這篇文章主要介紹了Android 中使用ContentObserver模式獲取短信用正則自動(dòng)填充驗(yàn)證碼,首先使用了ContentObserver監(jiān)聽(tīng)短信,然后從短信中用正則的分組去拿到驗(yàn)證碼,具體實(shí)現(xiàn)代碼大家參考下本文2017-02-02Android將應(yīng)用調(diào)試log信息保存在SD卡的方法
Android將應(yīng)用調(diào)試log信息保存在SD卡的方法大家都知道嗎,下面腳本之家小編給大家分享Android將應(yīng)用調(diào)試log信息保存在SD卡的方法,感興趣的朋友參考下2016-04-04Android中ExpandableListView使用示例詳解
這篇文章主要為大家詳細(xì)介紹了Android中ExpandableListView使用示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10