android 第三方分享

android 第三方分享_第1张图片android 第三方分享_第2张图片


MainActivity

 
  
package com.example.administrator.android_demo;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
    private  TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView= (TextView) findViewById(R.id.text1);
        textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"请重新更换密码",Toast.LENGTH_LONG).show();
            }
        });
        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                AndroidShare as = new AndroidShare(
                        MainActivity.this,
                        "哈哈---超方便的分享!!!来自allen",
                        "http://img6.cache.netease.com/cnews/news2012/img/logo_news.png");
                as.show();
            }
        });


    }

}

 
  
 
  
AndroidShare

package com.example.administrator.android_demo;

/**
 * Created by Administrator on 2016/3/25.
 */


import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Patterns;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidShare extends Dialog implements AdapterView.OnItemClickListener {
    private LinearLayout mLayout;
    private GridView mGridView;
    private float mDensity;
    private String msgText = "分享了...哈哈";
    private String mImgPath;
    private int mScreenOrientation;
    private List mListData;
    //private List wListData;
    private Handler mHandler = new Handler();

    private Runnable work = new Runnable() {
        public void run() {
            int orient = getScreenOrientation();
            if (orient != mScreenOrientation) {
                if (orient == 0)
                    mGridView.setNumColumns(4);
                else {
                    mGridView.setNumColumns(6);
                }
                mScreenOrientation = orient;
                ((AndroidShare.MyAdapter) mGridView.getAdapter()).notifyDataSetChanged();
            }
            mHandler.postDelayed(this, 1000L);
        }
    };

    public AndroidShare(Context context) {
        super(context, R.style.shareDialogTheme);
    }

    public AndroidShare(Context context, int theme, String msgText, final String imgUri) {
        super(context, theme);
        this.msgText = msgText;

        if (Patterns.WEB_URL.matcher(imgUri).matches())
            new Thread(new Runnable() {
                public void run() {
                    try {
                        mImgPath = getImagePath(imgUri, getFileCache());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        else
            this.mImgPath = imgUri;
    }

    public AndroidShare(Context context, String msgText, final String imgUri) {
        super(context, R.style.shareDialogTheme);
        this.msgText = msgText;

        if (Patterns.WEB_URL.matcher(imgUri).matches())
            new Thread(new Runnable() {
                public void run() {
                    try {
                        mImgPath = getImagePath(imgUri,getFileCache());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        else
            this.mImgPath = imgUri;
    }

    void init(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        dm = context.getResources().getDisplayMetrics();
        this.mDensity = dm.density;
        this.mListData = new ArrayList();
        this.mListData.add(new ShareItem("微信", R.drawable.logo_wechat,
                "com.tencent.mm.ui.tools.ShareImgUI", "com.tencent.mm"));
        this.mListData.add(new ShareItem("朋友圈", R.drawable.logo_wechatmoments,
                "com.tencent.mm.ui.tools.ShareToTimeLineUI", "com.tencent.mm"));
        this.mListData.add(new ShareItem("qq", R.drawable.logo_qq,
                "com.tencent.mobileqq.activity.JumpActivity","com.tencent.mobileqq"));
        this.mListData.add(new ShareItem("qq空间", R.drawable.logo_qzone,
                "com.qzone.ui.operation.QZonePublishMoodActivity","com.qzone"));
        this.mListData.add(new ShareItem("新浪微博", R.drawable.logo_sinaweibo,
                "com.sina.weibo.EditActivity", "com.sina.weibo"));
        this.mListData.add(new ShareItem("腾讯微博", R.drawable.logo_tencentweibo,
                "com.tencent.WBlog.intentproxy.TencentWeiboIntent","com.tencent.WBlog"));
        this.mListData.add(new ShareItem("其他", R.drawable.logo_other,
                "",""));

        this.mLayout = new LinearLayout(context);
        this.mLayout.setOrientation(1);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-1, -2);
        params.leftMargin = ((int) (10.0F * this.mDensity));
        params.rightMargin = ((int) (10.0F * this.mDensity));
        this.mLayout.setLayoutParams(params);
        this.mLayout.setBackgroundColor(Color.parseColor("#D9DEDF"));

        this.mGridView = new GridView(context);
        this.mGridView.setLayoutParams(new ViewGroup.LayoutParams(-1, -2));
        this.mGridView.setGravity(17);
        this.mGridView.setHorizontalSpacing((int) (10.0F * this.mDensity));
        this.mGridView.setVerticalSpacing((int) (10.0F * this.mDensity));
        this.mGridView.setStretchMode(1);
        this.mGridView.setColumnWidth((int) (90.0F * this.mDensity));
        this.mGridView.setHorizontalScrollBarEnabled(false);
        this.mGridView.setVerticalScrollBarEnabled(false);
        this.mLayout.addView(this.mGridView);
    }






    public List queryPackage() {
        List cns = new ArrayList();
        Intent i = new Intent("android.intent.action.SEND");
        i.setType("image/*");
        List resolveInfo = getContext().getPackageManager().queryIntentActivities(i, 0);
        for (ResolveInfo info : resolveInfo) {
            ActivityInfo ac = info.activityInfo;
            ComponentName cn = new ComponentName(ac.packageName, ac.name);
            cns.add(cn);
        }
        return cns;
    }

    public boolean isAvilible(Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();

        List pinfo = packageManager.getInstalledPackages(0);
        for (int i = 0; i < pinfo.size(); i++) {
            if (((PackageInfo) pinfo.get(i)).packageName.equalsIgnoreCase(packageName))
                return true;
        }
        return false;
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Context context = getContext();
        init(context);
        setContentView(this.mLayout);

        getWindow().setGravity(80);

        if (getScreenOrientation() == 0) {
            this.mScreenOrientation = 0;
            this.mGridView.setNumColumns(4);
        } else {
            this.mGridView.setNumColumns(6);
            this.mScreenOrientation = 1;
        }
        this.mGridView.setAdapter(new MyAdapter());
        this.mGridView.setOnItemClickListener(this);

        this.mHandler.postDelayed(this.work, 1000L);

        setOnDismissListener(new DialogInterface.OnDismissListener() {
            public void onDismiss(DialogInterface dialog) {
                mHandler.removeCallbacks(work);
            }
        });
    }

    public void show() {
        super.show();
    }

    public int getScreenOrientation() {
        int landscape = 0;
        int portrait = 1;
        Point pt = new Point();
        getWindow().getWindowManager().getDefaultDisplay().getSize(pt);
        int width = pt.x;
        int height = pt.y;
        return width > height ? portrait : landscape;
    }

    public void onItemClick(AdapterView parent, View view, int position, long id) {
        ShareItem share = (ShareItem) this.mListData.get(position);
        shareMsg(getContext(), "分享到...", this.msgText, this.mImgPath, share);
    }

    private void shareMsg(Context context, String msgTitle, String msgText,
                          String imgPath, ShareItem share) {
        if (!share.packageName.isEmpty() && !isAvilible(getContext(), share.packageName)) {
            Toast.makeText(getContext(), "请先安装" + share.title, Toast.LENGTH_SHORT).show();
            return;
        }

        Intent intent = new Intent("android.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");
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
            }
        }

        intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
        intent.putExtra(Intent.EXTRA_TEXT, msgText);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if(!share.packageName.isEmpty()) {
            intent.setComponent(new ComponentName(share.packageName,share.activityName));
            context.startActivity(intent);
        }
        else {
            context.startActivity(Intent.createChooser(intent, msgTitle));
        }
    }

    private File getFileCache() {
        File cache = null;

        if (Environment.getExternalStorageState().equals("mounted"))
            cache = new File(Environment.getExternalStorageDirectory() + "/." + getContext().getPackageName());
        else {
            cache = new File(getContext().getCacheDir().getAbsolutePath() + "/." + getContext().getPackageName());
        }
        if ((cache != null) && (!cache.exists())) {
            cache.mkdirs();
        }
        return cache;
    }

    public String getImagePath(String imageUrl, File cache) throws Exception {
        String name = imageUrl.hashCode() + imageUrl.substring(imageUrl.lastIndexOf("."));
        File file = new File(cache, name);

        if (file.exists()) {
            return file.getAbsolutePath();
        }

        URL url = new URL(imageUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        if (conn.getResponseCode() == 200) {
            InputStream is = conn.getInputStream();
            FileOutputStream fos = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            is.close();
            fos.close();

            return file.getAbsolutePath();
        }

        return null;
    }

    private final class MyAdapter extends BaseAdapter {
        private static final int image_id = 256;
        private static final int tv_id = 512;

        public MyAdapter() {
        }

        public int getCount() {
            return mListData.size();
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0L;
        }

        private View getItemView() {
            LinearLayout item = new LinearLayout(getContext());
            item.setOrientation(1);
            int padding = (int) (10.0F * mDensity);
            item.setPadding(padding, padding, padding, padding);
            item.setGravity(17);

            ImageView iv = new ImageView(getContext());
            item.addView(iv);
            iv.setLayoutParams(new LinearLayout.LayoutParams(-2, -2));
            iv.setId(image_id);

            TextView tv = new TextView(getContext());
            item.addView(tv);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-2, -2);
            layoutParams.topMargin = ((int) (5.0F * mDensity));
            tv.setLayoutParams(layoutParams);
            tv.setTextColor(Color.parseColor("#212121"));
            tv.setTextSize(16.0F);
            tv.setId(tv_id);

            return item;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getItemView();
            }
            ImageView iv = (ImageView) convertView.findViewById(image_id);
            TextView tv = (TextView) convertView.findViewById(tv_id);
            AndroidShare.ShareItem item = (AndroidShare.ShareItem) mListData.get(position);
            iv.setImageResource(item.logo);
            tv.setText(item.title);
            return convertView;
        }
    }

    private class ShareItem {
        String title;
        int logo;
        String activityName;
        String packageName;

        public ShareItem(String title, int logo, String activityName, String packageName) {
            this.title = title;
            this.logo = logo;
            this.activityName = activityName;
            this.packageName = packageName;
        }
    }
}


activity_main.xml

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:background="#36d9d1"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.android_demo.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:gravity="center"
        android:background="#af1b1b">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登陆"
            android:textColor="#ffffff"
            android:textSize="20sp"/>
    LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5">
        LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:paddingLeft="15dp"
            android:paddingRight="15dp">
            <EditText
                android:background="@drawable/edittext_ont"
                android:singleLine="true"
                android:hint="请输入手机号"
                android:textSize="20sp"
                android:paddingLeft="20dp"
                android:layout_width="match_parent"
                android:layout_height="50dp"


                />
        LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.2">

        LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp">
            <EditText
                android:hint="请输入密码"
                android:textSize="20sp"
                android:password="true"
                android:singleLine="true"
                android:paddingLeft="20dp"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@drawable/edittext_two"
                />
        LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="记住密码"
                android:textColor="#000000"
                android:textSize="18sp"/>
            <TextView
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="忘记密码"
                android:layout_marginLeft="150dp"
                android:textColor="#ff0000"
                android:textSize="18sp"/>
        LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
            <Button
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:text="登陆"
                android:background="@drawable/shape"
                android:textSize="20sp"
                android:textColor="#ffffff"/>
        LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2">
        LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5"
            android:gravity="center">
             <TextView
                 android:textSize="16sp"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="等三方登陆"
                 android:textColor="#000000"/>
        LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
            <LinearLayout
                android:gravity="center"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent">
                <Button
                    android:id="@+id/button1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="分享"
                    android:textSize="16sp"
                    android:background="@drawable/shape_button"/>

        LinearLayout>
     LinearLayout>
   LinearLayout>
LinearLayout>

<**俩个输入框和一个登陆按钮一个分享按钮的shape属性大致一样 就写一个shape布局了**>
shape

xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#ffffff"/>
    <corners android:radius="5sp"/>
    <stroke android:dashWidth="0.5dp" android:color="#333333"/>
shape>

anim.xml
slide_from_button.xml

xml version="1.0" encoding="UTF-8"?>
<set android:interpolator="@android:anim/decelerate_interpolator"
   xmlns:android="http://schemas.android.com/apk/res/android">
   <translate android:duration="500"
      android:fromYDelta="100.0%p" android:toYDelta="0.0" />
set>

anim.xml
slide_out_button.xml

xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator" >
    <translate
        android:duration="500"
        android:fromYDelta="0.0"
        android:toYDelta="100.0%p" />
set>




你可能感兴趣的:(原创)