zoukankan      html  css  js  c++  java
  • Android 实现分享功能的方法 分类: Android 2015-07-17 12:07 4人阅读 评论(0) 收藏

    第一种方法:特点–简单

    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/*"); 

  • 相关阅读:
    android之Animation
    A. Puzzles CodeForces Round #196 (Div.2)
    HDU 4662 MU Puzzle 2013 Multi-University Training Contest 6
    UVa 11464 Even Parity 偶数矩阵
    LA 3635 Pie 派 NWERC 2006
    UVa 11520 Fill the Square 填充正方形
    UVa 11384 Help is needed for Dexter 正整数序列
    HDU 4639 Hehe 2013 Multi-University Training Contest 4
    HDU 4627 The Unsolvable Problem 2013 Multi-University Training Contest 3
    HDU 4608 I-number 2013 Multi-University Training Contest 1
  • 原文地址:https://www.cnblogs.com/xieping/p/4666286.html
Copyright © 2011-2022 走看看