用Intent.ACTION_SEND進行分享

分享現在有許多集成的,如友盟,ShareSDK等,或者直接去下載哪個App的分享SDK來集成,這些分享都是會提示來自xxx(appName)。如果不需要,可以簡單通過Intent.ACTION_SEND來實現分享功能

分享

下面通過一段代碼來看看分享的實現

public static void share(Context context, String extraText) {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, extraText);
    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(
            Intent.createChooser(shareIntent, context.getString(R.string.action_share)));
}

可以看到,僅只需要傳送上下文和內容,一次分享動作就實現

  • 分享純文本
    如上面,調用ShareUtil.share(this, "test share");就實現了分享文本

  • 分享圖片

public static void share(Context context, Uri uri) {
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setType("image/*");
    context.startActivity(
            Intent.createChooser(shareIntent, context.getString(R.string.action_share)));
}

調用方式:

File file = BitmapUtil.Drawable2File(this, R.mipmap.ic_launcher, Environment.getExternalStorageDirectory() + "/test.png");
uri = BitmapUtil.File2Uri(file);
ShareUtil.share(this, uri);

注意:這裏有寫SD卡動作,需要添加讀寫權限,6.0添加運行時判斷

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  • 分享多張圖片
public static void share(Context context, ArrayList<Uri> uris) {
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
    shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    shareIntent.setType("image/*");
    context.startActivity(
            Intent.createChooser(shareIntent, context.getString(R.string.action_share)));
}

調用方式:ShareUtil.share(this, uris);

接收分享

從分享動作可以看出,只是發了一個Intent出來,所以其它App也是沒有硬性要求接收你的分享數據 – 不寫接口咯,但我們可以自己寫個接收分享數據接口出來,也是幾行代碼。

  • AndroidManifest.xml添加intent-filter
<activity android:name=".ShareReceiveActivity">
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="image/*"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND_MULTIPLE"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="image/*"/>
    </intent-filter>
</activity>
  • 分享接口
public class ShareReceiveActivity extends AppCompatActivity {

    @Bind(R.id.type)
    TextView type;
    @Bind(R.id.text)
    TextView text;
    @Bind(R.id.image)
    ImageView image;
    @Bind(R.id.image_multiple)
    ImageView imageMultiple;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_receive);
        ButterKnife.bind(this);
        // Get intent, action and MIME type
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();

        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {
                handleSendText(intent); // Handle text being sent
            } else if (type.startsWith("image/")) {
                handleSendImage(intent); // Handle single image being sent
            }
        } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
            if (type.startsWith("image/")) {
                handleSendMultipleImages(intent); // Handle multiple images being sent
            }
        } else {
            // Handle other intents, such as being started from the home screen
        }
    }

    void handleSendText(Intent intent) {
        String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
        if (sharedText != null) {
            // Update UI to reflect text being shared
            type.setText("type:text");
            text.setText(sharedText);
        }
    }

    void handleSendImage(Intent intent) {
        Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
        if (imageUri != null) {
            // Update UI to reflect image being shared
            type.setText("type:image");
            image.setImageURI(imageUri);
        }
    }

    void handleSendMultipleImages(Intent intent) {
        ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
        if (imageUris != null) {
            // Update UI to reflect multiple images being shared
            type.setText("type:multiple");
            if (imageUris.size() == 2) {
                image.setImageURI(imageUris.get(0));
                imageMultiple.setImageURI(imageUris.get(1));
            }
        }
    }
}

下載

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章