android 一个app分享内容到另外一个app,接受的app接受后并显示

来源:互联网 发布:长沙软件培训机构 编辑:程序博客网 时间:2024/06/01 09:45

基本原理:


分享的app应用程序里利用startActivity方法通过特定Action类型打开另外一个app应用程序的注册了特定Action类型的Activity;

这个activity启动后过getIntent()获取传过来的数据。

分享的app应用程序的主要代码是:

activity的代码是

setContentView(R.layout.activity_main);
button1 =(Button)findViewById(R.id.button1);
tv1 =(TextView)findViewById(R.id.tv1);
button1.setOnClickListener(item);

}
OnClickListener item = new OnClickListener() {


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent sendIntent = new Intent(); //创建意图
sendIntent.setAction(Intent.ACTION_SEND);//添加action
sendIntent.putExtra(Intent.EXTRA_TEXT, tv1.getText().toString());//吧分享的内容挂到Intent.EXTRA_TEXT标签上
sendIntent.setType("text/plain");//设置Intent类型
startActivity(sendIntent);
}


};

xml代码是:

<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"
    tools:context="${relativePackage}.${activityClass}" >


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="分享" />


    <EditText
        android:id="@+id/tv1"
        android:layout_width="fill_parent"
        android:layout_height="250dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:ems="10" />


</RelativeLayout>






接受分享内容的app应用程序代码主要代码是:

activity的代码是:



public class MainActivity extends Activity {


TextView tv1;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
Intent intent = getIntent();
if (intent != null) {
String action = intent.getAction();//获取action
String type = intent.getType();//获取intent的类型


if (Intent.ACTION_SEND.equals(action) && type != null) {//判断是发送类的Action
if ("text/plain".equals(type)) {//判断是文本类型
String sharedText = intent
.getStringExtra(Intent.EXTRA_TEXT);//通过Intent.EXTRA_TEXT标签获取发送过来的文本类型
if (sharedText != null) {
// 根据分享的文字更新UI
tv1.setText(sharedText); //显示文本信息
}


// 处理发送来的文字
} else if (type.startsWith("image/")) {


// 处理发送来的图片
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action)
&& type != null) {
if (type.startsWith("image/")) {
// 处理发送来的多张图片
}
} else {
// 处理其他intents,比如由主屏启动
}
}
}
}

在接受的app中重点要注册activity的过滤器:

在manifest文件中配置;

<activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <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" /> <!--  配置action为发送的类型 -->


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




0 0
原创粉丝点击