需求分析:在开发APP过程中,一般都需要实现分享转发,比如分享到QQ、微信等,这里我使用的是安卓系统自带的shareAPI,可以直接通过ResolveInfo获取手机中所有提供分享接口的应用,从而实现分享功能。
首先给大家看一下手机上的效果图:
Demo源代码下载地址:
https://download.csdn.net/download/qq15577969/10670207
一、xml布局文件
在res/values/styles.xml 中,添加以下代码,用来实现弹出窗背景效果
二、写一个实现类AppInfo.java ,用来保存应用信息
package com.t20.sharedemo.entity;
import android.graphics.drawable.Drawable;
public class AppInfo {
private String pkgName;
private String launchClassName;
private String appName;
private Drawable appIcon;
public String getPkgName() {
return pkgName;
}
public void setPkgName(String pkgName) {
this.pkgName = pkgName;
}
public String getLaunchClassName() {
return launchClassName;
}
public void setLaunchClassName(String launchClassName) {
this.launchClassName = launchClassName;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public Drawable getAppIcon() {
return appIcon;
}
public void setAppIcon(Drawable appIcon) {
this.appIcon = appIcon;
}
}
三、重写PopupWindow控件,自定义分享的弹窗
package com.t20.sharedemo.view;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.t20.sharedemo.R;
import com.t20.sharedemo.adapter.AppInfoAdapter;
import com.t20.sharedemo.entity.AppInfo;
import com.t20.sharedemo.util.ShareUtil;
import android.app.ActionBar.LayoutParams;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
public class SharePopupWindow extends PopupWindow {
//每行显示多少个
private static final int APP_SIZE = 8;
private View mMenuView;
private GridView mGridView;
private TextView mTextViewClose;
private AppInfoAdapter mAdapter;
private Intent shareIntent;
private List mAppinfoList;
private List mGridViewList;
/**
*
* @param context
* @param shareContent 要分享的内容
*/
public SharePopupWindow(final Context context,final String shareContent) {
super(context);
LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.share_dialog, null);
//获取控件
mGridView=(GridView) mMenuView.findViewById(R.id.sharePopupWindow_gridView);
mTextViewClose=(TextView) mMenuView.findViewById(R.id.sharePopupWindow_close);
//获取有分享功能的应用
shareIntent = new Intent(Intent.ACTION_SEND);
//shareIntent.setType("text/plain"); //纯文本
shareIntent.setType("*/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, shareContent);
mAppinfoList = ShareUtil.getShareAppList(context, shareIntent);
//适配GridView
mAdapter=new AppInfoAdapter(context, mAppinfoList);
mGridView.setAdapter(mAdapter);
//修改GridView
changeGridView(context);
mGridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
AppInfo appInfo=mAppinfoList.get(position);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setComponent(new ComponentName(appInfo.getPkgName(), appInfo.getLaunchClassName()));
//intent.setType("text/plain"); //纯文本
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TEXT, shareContent);
context.startActivity(intent);
}
});
//取消按钮
mTextViewClose.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
dismiss();
}
});
//设置SelectPicPopupWindow的View
this.setContentView(mMenuView);
//设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(LayoutParams.FILL_PARENT);
//设置SelectPicPopupWindow弹出窗体的高
this.setHeight(LayoutParams.WRAP_CONTENT);
//设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
//设置窗口外也能点击(点击外面时,窗口可以关闭)
this.setOutsideTouchable(true);
//设置SelectPicPopupWindow弹出窗体动画效果
this.setAnimationStyle(R.style.circleDialog);
//实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0x00000000);
//设置SelectPicPopupWindow弹出窗体的背景
this.setBackgroundDrawable(dw);
}
/**
* 将GridView改成单行横向布局
*/
private void changeGridView(Context context) {
// item宽度
int itemWidth = dip2px(context, 100);
// item之间的间隔
int itemPaddingH = dip2px(context, 1);
int size = mAppinfoList.size();
// 计算GridView宽度
int gridviewWidth = size/2 * (itemWidth + itemPaddingH);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
gridviewWidth, LinearLayout.LayoutParams.MATCH_PARENT);
mGridView.setLayoutParams(params);
mGridView.setColumnWidth(itemWidth);
mGridView.setHorizontalSpacing(itemPaddingH);
mGridView.setStretchMode(GridView.NO_STRETCH);
mGridView.setNumColumns(size/2);
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
* @param context 上下文
* @param dpValue dp值
* @return px值
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
四、再写一个工具类,用于获取手机中所有带分享功能的应用
package com.t20.sharedemo.util;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import java.util.ArrayList;
import java.util.List;
import com.t20.sharedemo.entity.AppInfo;
public class ShareUtil {
/**
* 获取手机内所有支持分享的应用列表
*/
public static ArrayList getShareAppList(Context context, Intent intent) {
ArrayList shareAppInfos = new ArrayList();
PackageManager packageManager = context.getPackageManager();
List resolveInfos = getShareApps(context);
if (null == resolveInfos) {
return null;
} else {
for (ResolveInfo resolveInfo : resolveInfos) {
AppInfo appInfo = new AppInfo();
appInfo.setPkgName(resolveInfo.activityInfo.packageName);
appInfo.setLaunchClassName(resolveInfo.activityInfo.name);
appInfo.setAppName(resolveInfo.loadLabel(packageManager).toString());
appInfo.setAppIcon(resolveInfo.loadIcon(packageManager));
shareAppInfos.add(appInfo);
}
}
return shareAppInfos;
}
/**
* 查询手机内所有支持分享的应用列表
*/
public static List getShareApps(Context context) {
List mApps = new ArrayList();
Intent intent = new Intent(Intent.ACTION_SEND, null);
intent.addCategory(Intent.CATEGORY_DEFAULT);
//intent.setType("text/plain"); //纯文本
intent.setType("*/*");
PackageManager pManager = context.getPackageManager();
mApps = pManager.queryIntentActivities(intent,PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
return mApps;
}
}
六、我是使用GridView来显示应用分享图标的,因此需要写个GridView的适配器
package com.t20.sharedemo.adapter;
import java.util.List;
import com.t20.sharedemo.R;
import com.t20.sharedemo.entity.AppInfo;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class AppInfoAdapter extends BaseAdapter {
private Context context;
private List mAppinfoList;
private OnItemClickListener mOnItemClickLitener;
public AppInfoAdapter(Context context, List mAppinfoList) {
super();
this.context = context;
this.mAppinfoList = mAppinfoList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mAppinfoList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@SuppressLint("NewApi")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
AppInfo appInfo = mAppinfoList.get(position);
// 加载布局
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(context).inflate(R.layout.appinfo_item, null);
viewHolder = new ViewHolder(view);
// 将ViewHolder存储在View中
view.setTag(viewHolder);
} else {
view = convertView;
// 重新获取ViewHolder
viewHolder = (ViewHolder) view.getTag();
}
//设置控件的值
viewHolder.imageViewIcon.setImageDrawable(appInfo.getAppIcon());
String name=appInfo.getAppName();
viewHolder.textViewName.setText(name);
return view;
}
class ViewHolder {
ImageView imageViewIcon;
TextView textViewName;
public ViewHolder(View view) {
this.imageViewIcon = (ImageView) view.findViewById(R.id.appinfo_item_icon);
this.textViewName = (TextView) view.findViewById(R.id.appinfo_item_name);
}
}
}
七、MainActivity.java活动的代码比较简单,直接定义SharePopupWindow窗口即可
package com.t20.sharedemo;
import com.t20.sharedemo.view.SharePopupWindow;
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
private RelativeLayout mLayoutRoot;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 1、隐藏标题栏,在加载布局之前设置(兼容Android2.3.3版本)
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 2、隐藏状态栏
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//获取根布局
mLayoutRoot=(RelativeLayout) findViewById(R.id.mainActivity_root);
}
/**
* 点击进行分享
*/
public void share(View view){
// 设置要分享的内容
String shareContent="#神码工作室#博客地址:https://blog.csdn.net/qq15577969";
SharePopupWindow spw = new SharePopupWindow(this, shareContent);
// 显示窗口
spw.showAtLocation(mLayoutRoot, Gravity.BOTTOM, 0, 0);
}
}