Android Share App Data (ACTION_SEND)
Android使用 ACTION_SEND 事件來發(fā)送數(shù)據(jù),其屬于 android.content.Intent 類,可以將數(shù)據(jù)從一個(gè)活動(dòng)發(fā)送到另一個(gè)活動(dòng)以及從當(dāng)前活動(dòng)發(fā)送到應(yīng)用程序外部。愛掏網(wǎng) - it200.comIntent類需要指定要共享的數(shù)據(jù)及其類型。愛掏網(wǎng) - it200.com
通常情況下,ACTION_SEND操作會(huì)發(fā)送內(nèi)置瀏覽器應(yīng)用程序的URL。愛掏網(wǎng) - it200.com在共享數(shù)據(jù)時(shí),Intent調(diào)用createChooser()方法,該方法接收Intent對象并指定選擇器對話框的標(biāo)題。愛掏網(wǎng) - it200.com Intent.createChooser()方法用于顯示選擇器。愛掏網(wǎng) - it200.com
ACTION_SEND示例
在此示例中,我們將共享純文本,即瀏覽器的URL鏈接。愛掏網(wǎng) - it200.com
activity_main.xml
文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.shareapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share App"
android:id="@+id/button"
android:layout_marginBottom="95dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Activity類
文件:MainActivity.java
package com.example.test.shareapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button sharebutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharebutton=(Button)findViewById(R.id.button);
sharebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Insert Subject here");
String app_url = " https://play.google.com/store/apps/details?id=my.example.javatpoint";
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,app_url);
startActivity(Intent.createChooser(shareIntent, "Share via"));
}
});
}
}
輸出
聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。