Android分享功能

package com.ghstudio.sharetest;  
import java.util.List;  
import android.app.Activity;  
import android.content.Intent;  
import android.content.pm.PackageManager;  
import android.content.pm.ResolveInfo;  
import android.os.Bundle;  
import android.view.Menu;  
import android.view.MenuItem;  
public class Main extends Activity {  
@Override  
public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.main);   
} 
/* 创建菜单 */  
public boolean onCreateOptionsMenu(Menu menu) {  
  menu.add(0,0,0,"分享");   
  return true;  
}  
public boolean onOptionsItemSelected(MenuItem item){  
  switch (item.getItemId()){  
  case 0:  
    Intent intent=new Intent(Intent.ACTION_SEND);  
    //intent.setType("text/plain");  //纯文本
    /*图片分享
    it.setType("image/png");
     //添加图片
      File f = new File(Environment.getExternalStorageDirectory()
        +"/Pictures/2.png");
      Uri u = Uri.fromFile(f);
      it.putExtra(Intent.EXTRA_STREAM, u);
     */

    intent.putExtra(Intent.EXTRA_SUBJECT, "分享");  
    intent.putExtra(Intent.EXTRA_TEXT, "I would like to share this with you...");  
    startActivity(Intent.createChooser(intent, getTitle()));  
    return true;  
  }  
  return false;  
}  
}


Android分享功能的代码解析:首先创建一个Options菜单,该菜单只有一个项“分享”。当点击菜单项时,创建一个Intent。该Intent设置为发送给支持ACTION_SEND的Activity。用两个putExtra给Intent设置了SUBJECT和TEXT的数据,再用startActivity方法让系统调用适当的Activity执行之。createChooser方法接受Intent做参数,也同时接纳了Intent里面要求的filter(ACTION_SEND),只有支持ACTION_SEND的Activity才会被列入可选列表。


Share via Facebook

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Content to share");
PackageManager pm = v.getContext().getPackageManager();
List activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
  if ((app.activityInfo.name).contain("facebook")) {
    final ActivityInfo activity = app.activityInfo;
    final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
    shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |       Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    shareIntent.setComponent(name);
    v.getContext().startActivity(shareIntent);
    break;
 }
}

Bonus - Share via Twitter

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Content to share");
PackageManager pm = v.getContext().getPackageManager();
List activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
  if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) {
    final ActivityInfo activity = app.activityInfo;
    final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
    shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |       Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    shareIntent.setComponent(name);
    v.getContext().startActivity(shareIntent);
    break;
 }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值