亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

簡述Android中實現(xiàn)APP文本內(nèi)容的分享發(fā)送與接收方法

 更新時間:2016年12月19日 15:48:18   作者:潘侯爺  
本篇文章主要對Android中實現(xiàn)APP文本內(nèi)容的分享發(fā)送與接收方法進(jìn)行介紹,相信對大家學(xué)習(xí)會有很好的幫助,需要的朋友一起來看下吧

謹(jǐn)記(指定選擇器Intent.createChooser())

開始今天的內(nèi)容前,先閑聊一下:

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

(2)在網(wǎng)上發(fā)現(xiàn)一段特別好的文章,想要保存收藏下來。

上面描述了進(jìn)入智能手機(jī)時代后,我們經(jīng)常遇到的兩種情況,那么作為開發(fā)者的我們?nèi)绾巫屪约洪_發(fā)的APP實現(xiàn)這兩種功能呢,下面我們以實現(xiàn)文本的發(fā)送分享以及接收來梳理下兩種功能的實現(xiàn)過程(其他類型的數(shù)據(jù)在博文末尾會給大家做簡單介紹)。

第一種情況:APP實現(xiàn)發(fā)送分享文本功能

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

(1)指定選擇器的實現(xiàn)效果如下:

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

(2)未指定選擇器的實現(xiàn)效果如下:

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

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

第一步: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="想給潘侯爺說點什么"/>
 <Button
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:onClick="sendtext"
 android:text="發(fā)送文本" />
</LinearLayout>

第二步:Java中實現(xiàn)代碼MainActivity.java功能實現(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)首次選擇后,后期將無法再次改變
// startActivity(intent);
 //指定選擇器選擇使用有發(fā)送文本功能的App
 startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name)));
 }
}

第二種情況:APP實現(xiàn)接收分享文本功能

實現(xiàn)效果如下(將短信內(nèi)容分享到我們的APP上):

layout布局界面為初始默認(rèn),僅一個默認(rèn)helloworld的TextView界面,這里就省略不寫了。

第一步: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中實現(xiàn)代碼MainActivity.java功能實現(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)));
}

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

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

  • Android中webView加載H5綁定cookie實例

    Android中webView加載H5綁定cookie實例

    這篇文章主要介紹了Android中webView加載H5綁定cookie實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android  隱式Intent的實例詳解

    Android 隱式Intent的實例詳解

    這篇文章主要介紹了Android 隱式Intent的實例詳解的相關(guān)資料,隱式意圖就是在不明確設(shè)置激活對象的前提下尋找最匹配的組件,需要的朋友可以參考下
    2017-08-08
  • Android實現(xiàn)開機(jī)自動啟動Service或app的方法

    Android實現(xiàn)開機(jī)自動啟動Service或app的方法

    這篇文章主要介紹了Android實現(xiàn)開機(jī)自動啟動Service或app的方法,結(jié)合實例形式分析了Android開機(jī)自啟動程序的具體步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2016-07-07
  • Android通過記住密碼功能學(xué)習(xí)數(shù)據(jù)存儲類SharedPreferences詳解及實例

    Android通過記住密碼功能學(xué)習(xí)數(shù)據(jù)存儲類SharedPreferences詳解及實例

    這篇文章主要通過“記住密碼”實例功能學(xué)習(xí)為大家介紹了Android數(shù)據(jù)存儲類SharedPreferences,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Kotlin如何捕獲上下文中的變量與常量詳解

    Kotlin如何捕獲上下文中的變量與常量詳解

    這篇文章主要給大家介紹了關(guān)于Kotlin如何捕獲上下文中的變量與常量的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Kotlin具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Android繪制驗證碼的實例代碼

    Android繪制驗證碼的實例代碼

    這篇文章主要介紹了Android繪制驗證碼的實例代碼,需要的朋友可以參考下
    2017-07-07
  • Andriod事件分發(fā)事件由來初識

    Andriod事件分發(fā)事件由來初識

    這篇文章主要為大家講解了Andriod事件分發(fā)事件由來的初步認(rèn)識,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Android 中使用ContentObserver模式獲取短信用正則自動填充驗證碼

    Android 中使用ContentObserver模式獲取短信用正則自動填充驗證碼

    這篇文章主要介紹了Android 中使用ContentObserver模式獲取短信用正則自動填充驗證碼,首先使用了ContentObserver監(jiān)聽短信,然后從短信中用正則的分組去拿到驗證碼,具體實現(xiàn)代碼大家參考下本文
    2017-02-02
  • Android將應(yīng)用調(diào)試log信息保存在SD卡的方法

    Android將應(yīng)用調(diào)試log信息保存在SD卡的方法

    Android將應(yīng)用調(diào)試log信息保存在SD卡的方法大家都知道嗎,下面腳本之家小編給大家分享Android將應(yīng)用調(diào)試log信息保存在SD卡的方法,感興趣的朋友參考下
    2016-04-04
  • Android中ExpandableListView使用示例詳解

    Android中ExpandableListView使用示例詳解

    這篇文章主要為大家詳細(xì)介紹了Android中ExpandableListView使用示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10

最新評論