• Android自带的分享功能案例


    MainActivity的代码

    package com.hpsvse.weiboshare;
    import java.io.File;
    import android.net.Uri;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;


    public class MainActivity extends Activity {
    private Button btn1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    }
    private void initView() {
    // TODO Auto-generated method stub
    btn1=(Button) findViewById(R.id.button1);
    btn1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    shareMsg(MainActivity.this,null,null,null,null);

    }
    });
    }
    public static void shareMsg(Context context, String activityTitle, String msgTitle, String msgText,
    String imgPath) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    if (imgPath == null || imgPath.equals("")) {
    intent.setType("text/plain"); // 纯文本
    } else {
    File f = new File(imgPath);
    if (f != null && f.exists() && f.isFile()) {
    intent.setType("image/png");
    Uri u = Uri.fromFile(f);
    intent.putExtra(Intent.EXTRA_STREAM, u);
    }
    }
    intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
    intent.putExtra(Intent.EXTRA_TEXT, msgText);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(Intent.createChooser(intent, activityTitle));
    }
    }

    activity_main.xml 代码

    <LinearLayout 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=".MainActivity"
        android:orientation="vertical" >
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="分享" />

    </LinearLayout>

    其它的文件都不变

    值得注意的是。你分享的内容的多少取决你的手机有多少社交软件,


  • 相关阅读:
    gorm使用小结
    golang 输入输出
    nginx 命令和配置
    设计模式
    并发
    Java教程
    Spring实战
    第12章 高级数据结构及其实现
    第10章 算法设计技巧
    第9章 图论算法
  • 原文地址:https://www.cnblogs.com/llguanli/p/6897264.html
Copyright © 2020-2023  润新知