日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

android intent 分发,Android分享操作

發(fā)布時(shí)間:2024/9/30 Android 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android intent 分发,Android分享操作 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

現(xiàn)在用的比較多的都是三方的分享,其實(shí)安卓自帶的就有簡(jiǎn)單的分享。

在構(gòu)建Intent時(shí),可以指定這個(gè)Intent需要觸發(fā)的actions。比如ACTION_SEND,該action表明該intent用于從一個(gè)activity發(fā)送數(shù)據(jù)到另外一個(gè)activity的,甚至可以跨進(jìn)程之間的數(shù)據(jù)發(fā)送。系統(tǒng)會(huì)自動(dòng)識(shí)別出能夠兼容接受的這些數(shù)據(jù)的activity。如果這些選擇有多個(gè),則把這些activity顯示給用戶進(jìn)行選擇;如果只有一個(gè),則立即啟動(dòng)該Activity。

分享數(shù)據(jù)

分享簡(jiǎn)單的數(shù)據(jù)

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

在不同的程序之間使用intent收發(fā)數(shù)據(jù)是在社交分享內(nèi)容時(shí)最常用的方法。

若有多個(gè)匹配的程序,則系統(tǒng)會(huì)把他們都給篩選出來(lái),并呈現(xiàn)Dialog給用戶進(jìn)行選擇。

如果設(shè)備上安裝有某個(gè)能夠匹配ACTION_SEND且MIME類型為text/plain的程序,則Android系統(tǒng)會(huì)立即執(zhí)行它。

不過(guò)為intent調(diào)用了Intent.createChooser(),那么Android總是會(huì)顯示可供選擇:

startActivity(Intent.createChooser(sendIntent,getResources().getText(R.string.send_to)));

看下顯示效果:

image.png

image.png

image.png

分享圖片

如果想要分享圖片不是文字的話,首先得將setType設(shè)置成“image/jpeg”,如果不確定圖片類型直接使用"image/*,同時(shí)數(shù)據(jù)需要結(jié)合設(shè)置特定的MIME類型,EXTRA_STREAM里面放置數(shù)據(jù)的URI。

Intent sendIntent = new Intent();

sendIntent.setAction(Intent.ACTION_SEND);

sendIntent.putExtra(Intent.EXTRA_STREAM, mCroppedImageFile.getPath());

sendIntent.setType("image/");

startActivity(Intent.createChooser(sendIntent, "分享"));

image.png

image.png

image.png

分享多種類型數(shù)據(jù)

如果分享3張JPEG的圖片,那么MIME類型仍然是image/jpeg。如果是不同圖片格式的話,應(yīng)該是用image/來(lái)匹配那些可以接收任何圖片類型的activity。如果需要分享多種不同類型的數(shù)據(jù),可以使用/*來(lái)表示MIME。

一次分享多張圖片

ArrayList imageUris = new ArrayList<>();

imageUris.add(Uri.fromFile(mCroppedImageFile));

imageUris.add(Uri.fromFile(mCroppedImageFile));

Intent sendIntent = new Intent();

sendIntent.setAction(Intent.ACTION_SEND_MULTIPLE);

sendIntent.putExtra(Intent.EXTRA_STREAM, imageUris);

sendIntent.setType("image/*");

startActivity(Intent.createChooser(sendIntent, "分享"));

可以看到同時(shí)分發(fā)了2張圖片:

image.png

接收從其他App傳送來(lái)的數(shù)據(jù)

讓自己的app可以接受數(shù)據(jù)

Intent filters告訴Android系統(tǒng)一個(gè)程序愿意接受的數(shù)據(jù)類型。

android:name=".MainActivity"

android:theme="@style/CustomActionBarTheme">

上面將當(dāng)前app的MainActivity可以接受文字。然后我們用另外一個(gè)app來(lái)分享文字:

image.png

上圖中的TraningApp就是我自己定義能接受文字分享的app

處理接受到的數(shù)據(jù)

為了處理從Intent帶來(lái)的數(shù)據(jù),可以通過(guò)調(diào)用getIntent()方法來(lái)獲取到Intent對(duì)象。拿到這個(gè)對(duì)象后,我們可以對(duì)其中面的數(shù)據(jù)進(jìn)行判斷,從而決定下一步行為。

Intent intent = getIntent();

String action = intent.getAction();

String type = intent.getType();

if (Intent.ACTION_SEND.equals(action)){

if ("text/plain".equals(type)){

Log.e(TAG, "ACTION_SEND:"+intent.getStringExtra(Intent.EXTRA_TEXT) );

}

}

然后發(fā)送消息:

Intent sendIntent = new Intent();

sendIntent.setAction(Intent.ACTION_SEND);

sendIntent.putExtra(Intent.EXTRA_TEXT, "測(cè)試文字");

sendIntent.setType("text/plain");

startActivity(Intent.createChooser(sendIntent, "分享"));

查看log日志:

07-26 12:32:51.315 9746-9746/com.example.frc.trainingapp E/MainActivity: ACTION_SEND:測(cè)試文字

同樣我們可以添加圖片的處理:

if (Intent.ACTION_SEND.equals(action)) {

if ("text/plain".equals(type)) {

Log.e(TAG, "ACTION_SEND:" + intent.getStringExtra(Intent.EXTRA_TEXT));

} else if ("image/*".equals(type)) {

Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);

Log.e(TAG, "ACTION_SEND_URI:"+uri.toString());

}

}

查看Log日志:

07-26 14:09:37.135 30944-30944/com.yanxiu.yxsanke_android E/SK::: /storage/emulated/0/YXSanKe_Android/res/nnnn.jpg

note:需要注意的是處理發(fā)送過(guò)來(lái)的數(shù)據(jù)可能會(huì)是耗時(shí)操作,建議不要在UI線程進(jìn)行

總結(jié)

以上是生活随笔為你收集整理的android intent 分发,Android分享操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。