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>
- android 一个app分享内容到另外一个app,接受的app接受后并显示
- android 另外一个app
- android app启动另外一个app
- 启动另外一个app
- 一个android app打开另外一个app,并传替数据(个人学习笔记)
- Android 如何从一个APP中打开另外一个APP
- Android 如何从一个APP中打开另外一个APP
- 一个app内部启动另外一个app
- 一个APP吊起另外一个APP
- 在一个app中启动另外一个app的service
- android中一个app调用另外一个app中Activity的编程实例
- 安卓app启动另外一个app
- Android 神奇的一个app跳转到另一个app
- android 分享内容到第三方app
- Android进程间互相调用,一个APP调用另外的APP
- Android | 将内容分享到自己的app
- android中实现一个APP启动另一个APP并实现类似微信分享
- 通过一个App去调用另外一个App
- Java Stream(1)
- STM32 时钟系统和内部结构
- Toolbar的三个问题:修改左边箭头颜色、怎样修改右边以及子activity中的toolbar添加返回箭头
- NSString方法compare详解
- 代码管理
- android 一个app分享内容到另外一个app,接受的app接受后并显示
- mysql中[Err] 1093
- 【带权并查集】 练习
- 【Java学习笔记】11.内部类
- request如何获取IP
- oracle数据库操作语句
- 迷宫出口以及迷宫最短路径的求解
- python二叉查找树
- html5移动场景网站