如何给应用增加分享功能,怎样将应用加入系统的分享选择列表?

Intent.createChooser()方法用来弹出系统分享列表。

查看Intent对应的组件是否存在,可查看Android判断Intent是否存在,是否可用

1、应用增加分享功能

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class FenxiangActivity extends Activity {    private Button btnFenXiang = null;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        btnFenXiang = (Button) findViewById(R.id.btnFenXiang);        btnFenXiang.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(Intent.ACTION_SEND); // 启动分享发送的属性                intent.setType("text/plain"); // 分享发送的数据类型                intent.putExtra(Intent.EXTRA_SUBJECT, "subject"); // 分享的主题                intent.putExtra(Intent.EXTRA_TEXT, "extratext"); // 分享的内容                //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// 这个也许是分享列表的背景吧                FenxiangActivity.this.startActivity(Intent.createChooser(                        intent, "分享"));// 目标应用选择对话框的标题            }        });    }

上面的代码为分享文本,若想分享图片信息需要设置setType为“image/*”,传递一个类型为Uri的参数Intent.EXTRA_STREAM。

2、应用加入系统分享列表
只需在AndroidManifest.xml中加入以下代码:

1
2
3
4
5
6
7
<activityandroid:name=".SharePage"android:label="分享到微博">
<intent-filter>
<actionandroid:name="android.intent.action.SEND"/>
<categoryandroid:name="android.intent.category.DEFAULT"/>
<dataandroid:mimeType="image/*"/>
</intent-filter>
</activity>

下载 地址 :http://files.cnblogs.com/firecode/Fenxiang.rar

Android:“分享到邮箱”的实现

这是一个简单的分享到邮箱的实现,你可以附上自己的图片。

  1. //cacheDir是你所要共享的文件对象所在的目录
  2. //你可以用自己的文件对象覆盖File f
  3. FilecacheDir=newFile(android.os.Environment.getExternalStorageDirectory(),getString(getApplicationInfo().labelRes));
  4. Filef=newFile(cacheDir,"image_name.jpg");
  5. Intentintent=newIntent(Intent.ACTION_SEND);
  6. intent.setType("image/jpeg");
  7. intent.putExtra(Intent.EXTRA_TEXT,"Emailbodyoverhere");
  8. intent.putExtra(Intent.EXTRA_SUBJECT,"Emailsubjectoverhere");
  9. intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(f));
  10. startActivity(Intent.createChooser(intent,"Sharevia:"));

更多相关文章

  1. Android中Toast的用法简介(转)
  2. Android编译系统入门(二)
  3. android材料设计语言
  4. 史上最全selector和shape使用方法 Android(安卓)ListView 列表项
  5. 修改系统action bar字体大小、粗细、颜色等样式的方法
  6. 解决:修改安卓system系统文件导致手机一直重启
  7. Android(安卓)如何获取经纬度
  8. Android的Style的使用
  9. android zipalign对齐优化学习

随机推荐

  1. Android获取手机应用程序包的信息
  2. android 基本概念和一些术语
  3. Android中改变一个图片的透明度
  4. 2011.09.26(2)——— android sample之Note
  5. android HTTP 登陆链接Mysql数据库实现登
  6. Didn't find class "androidx.core.conte
  7. 阅读《Android 从入门到精通》(2)——熟悉
  8. Android使用MQTT协议
  9. Android中实现整个视图切换的左右滑动效
  10. Android --spinner构造下垃菜单