Android 实现分享功能的方法 分类: Android ...

第一种方法:特点–简单

package com.example.share;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

/* 创建菜单 */
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "分享");
return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
// intent.setType("text/plain"); //纯文本
/*
* 图片分享 it.setType("image/png");  //添加图片 File f = new
* File(Environment.getExternalStorageDirectory()+"/name.png");
*
* Uri uri = Uri.fromFile(f); intent.putExtra(Intent.EXTRA_STREAM,
* uri);
*/
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_SUBJECT, "Share");
intent.putExtra(Intent.EXTRA_TEXT, "I have successfully share my message through my app");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, getTitle()));
return true;
}
return false;
}
}
第二种方法:特点–全面
在SHARESDK官网中下载 shareSDK for android 功能开发包
http://share.sharesdk.cn/Download
参考链接:http://www.mikel.cn/%E5%BC%80%E5%8F%91%E7%AC%94%E8%AE%B0/%E8%BD%AC%E8%BD%BDandroid-%E5%AE%9E%E7%8E%B0%E5%88%86%E4%BA%AB%E5%8A%9F%E8%83%BD%E4%B8%A4%E7%A7%8D%E6%96%B9%E6%B3%95-%E5%B0%8F%E9%A3%92-%E5%8D%9A%E5%AE%A2%E5%9B%AD.html


intent=new Intent(Intent.ACTION_SEND); 
intent.setType("text/plain"); 
intent.putExtra(Intent.EXTRA_SUBJECT, "分享"); 
intent.putExtra(Intent.EXTRA_TEXT, text);  
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(Intent.createChooser(intent, getTitle())); 
上面是很多网站说的分享功能,但是我写了却不能够分享到腾讯微博还有开心网。怎么解决把内容分享到这个两个平台上去。 
上面的代码是能够分享到人人网,新浪微博,短信上的
  1. public void onClickShare(View view) {  
  2.   
  3.         Intent intent=new Intent(Intent.ACTION_SEND);   
  4.         intent.setType("image/*");   
  5.         intent.putExtra(Intent.EXTRA_SUBJECT, "分享");   
  6.         intent.putExtra(Intent.EXTRA_TEXT, "终于可以了!!!");    
  7.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
  8.         startActivity(Intent.createChooser(intent, getTitle()));   
  9.   
  10.     }  
send的activity 的配置为 
<activity android:name=".activity.MicroBlogInput" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation" android:windowSoftInputMode="stateAlwaysVisible|adjustResize"> 
            <intent-filter android:label="@string/albums_sendbyWBlog"> 
                <action android:name="android.intent.action.SEND" /> 
                <data android:mimeType="image/*" />
                
               <category android:name="android.intent.category.DEFAULT" /> 
            </intent-filter> 
        </activity> 
它并没有定义date mimetype=("text/plain"); 
所以intent.setType("text/plain"); 
这个条件就把它过滤掉了 

所以你的代码要把intent.setType("text/plain"); 
这句改为 
intent.setType("imge/*"); 

转载于:https://www.cnblogs.com/xieping/p/4666286.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值