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

Android調(diào)用系統(tǒng)自帶的分享功能實(shí)例代碼

 更新時(shí)間:2017年04月25日 09:20:16   作者:姜康  
本篇文章主要介紹了Android調(diào)用系統(tǒng)自帶的分享功能實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

實(shí)現(xiàn)分享功能的幾個(gè)辦法

1.調(diào)用系統(tǒng)的分享功能

2.通過(guò)第三方SDK,如ShareSDK,友盟等

3.自行使用各自平臺(tái)的SDK,比如QQ,微信,微博各自的SDK

這里就記錄下第一種辦法。

分享文本信息

    Intent textIntent = new Intent(Intent.ACTION_SEND);
    textIntent.setType("text/plain");
    textIntent.putExtra(Intent.EXTRA_TEXT, "這是一段分享的文字");
    startActivity(Intent.createChooser(textIntent, "分享"));

效果如下圖:

分享單張圖片

    String path = getResourcesUri(R.drawable.shu_1);
    Intent imageIntent = new Intent(Intent.ACTION_SEND);
    imageIntent.setType("image/jpeg");
    imageIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path));
    startActivity(Intent.createChooser(imageIntent, "分享"));

分享多個(gè)文件

    ArrayList<Uri> imageUris = new ArrayList<>();
    Uri uri1 = Uri.parse(getResourcesUri(R.drawable.dog));
    Uri uri2 = Uri.parse(getResourcesUri(R.drawable.shu_1));
    imageUris.add(uri1);
    imageUris.add(uri2);
    Intent mulIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    mulIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
    mulIntent.setType("image/jpeg");
    startActivity(Intent.createChooser(mulIntent,"多文件分享"));

上面幾個(gè)例子的效果都是一樣的,創(chuàng)建一個(gè)選擇器,讓用戶(hù)自己選擇分享到哪里。

這里有一點(diǎn)得注意,就是通過(guò)這種方法進(jìn)行分享,Intent傳遞的數(shù)據(jù)的Type(就是setType()方法)一定要控制好,不然會(huì)出錯(cuò)。(至于為什么后面說(shuō))。

其中由于是分享的res中的圖片,故轉(zhuǎn)變?yōu)閡ri,方法在這:

 private String getResourcesUri(@DrawableRes int id) {
  Resources resources = getResources();
  String uriPath = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
    resources.getResourcePackageName(id) + "/" +
    resources.getResourceTypeName(id) + "/" +
    resources.getResourceEntryName(id);
  Toast.makeText(this, "Uri:" + uriPath, Toast.LENGTH_SHORT).show();
  return uriPath;
 }

指定分享到微信

    Intent wechatIntent = new Intent(Intent.ACTION_SEND);
    wechatIntent.setPackage("com.tencent.mm");
    wechatIntent.setType("text/plain");
    wechatIntent.putExtra(Intent.EXTRA_TEXT, "分享到微信的內(nèi)容");
    startActivity(wechatIntent);

效果如下:

指定分享到QQ

    Intent qqIntent = new Intent(Intent.ACTION_SEND);
    qqIntent.setPackage("com.tencent.mobileqq");
    qqIntent.setType("text/plain");
    qqIntent.putExtra(Intent.EXTRA_TEXT, "分享到微信的內(nèi)容");
    startActivity(qqIntent);

效果如下:

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

相關(guān)文章

最新評(píng)論