这几天公司项目需要有分享功能,于是自己就模仿了新浪微博客户端做了一下,简单的做了四个分享。
没图你说个杰宝~~~上截图~~
这里主要用到的是PopWindow。
下面看撸主的代码~~
ShareMenu.java
public class ShareMenu{ private Context context; private View view; private TextView txtCancel; private GridView gridView; private PopupWindow popupWindow; private String[] shareNames; private String[] sharePackageNames; private String[] shareActicityNames; private int[] shareIcons; private File appDir; public ShareMenu(Context context){ this.context = context; shareNames = new String[]{"微博","朋友圈","微信好友","QQ空间"}; sharePackageNames = new String[]{ "com.sina.weibo", "com.tencent.mm", "com.tencent.mm", "com.qzone"}; shareActicityNames = new String[]{ "com.sina.weibo.ComposerDispatchActivity", "com.tencent.mm.ui.tools.ShareToTimeLineUI", "com.tencent.mm.ui.tools.ShareImgUI", "com.qzone.ui.operation.QZonePublishMoodActivity"}; shareIcons = new int[]{ R.drawable.more_weibo, R.drawable.more_circlefriends, R.drawable.more_weixin, R.drawable.more_qzone}; //创建应用的目录 appDir = FileUtil.createDir("ShareMenu",Environment.getExternalStorageDirectory().getAbsolutePath()); initView(); } /** * 初始化控件 */ private void initView(){ view = LayoutInflater.from(context).inflate(R.layout.layout_share_menu, null); gridView = (GridView) view.findViewById(R.id.layout_share_menu_grid); txtCancel = (TextView) view.findViewById(R.id.layout_share_menu_txt_cancel); gridView.setAdapter(new ShareAdapter(context, shareIcons, shareNames)); popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); // 这个是为了点击“返回/Back”也能使其消失,并且并不会影响你的背景 ColorDrawable dw = new ColorDrawable(Color.WHITE); popupWindow.setBackgroundDrawable(dw); //网格的单击事件 gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) { // 判断是否安装了该应用程序 if (isExistApp(sharePackageNames[position])) { Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setComponent(new ComponentName(sharePackageNames[position], shareActicityNames[position])); //设置类型 shareIntent.setType("image/*"); File file = shotScreen(context); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(shareIntent); }else { Toast.makeText(context, "未安装该应用", Toast.LENGTH_SHORT).show(); } } }); //"取消"按钮的监听事件 txtCancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dismiss(); } }); } /** * 截图并保存到本地 * @return */ public File shotScreen(Context context){ Activity activity = (Activity)context; //获取屏幕截图,这种截图方式不能截取状态栏 View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); //创建截图文件 File shotScreen = FileUtil.createFile("ShotScreen.png", appDir.getPath()); FileOutputStream fos = null; try { fos = new FileOutputStream(shotScreen); } catch (FileNotFoundException e) { e.printStackTrace(); } //将截图保存到本地,参数分别是截图的格式,图片的质量(百分比),输出流 bmp.compress(Bitmap.CompressFormat.PNG, 100, fos); return shotScreen; } /** * 检索手机是否安装了该应用程序 * @param packageName * @return */ public boolean isExistApp(String packageName){ PackageManager pm = context.getPackageManager(); //0表示第三方应用,非0表示系统应用 List<PackageInfo> list = pm.getInstalledPackages(0); for (PackageInfo packageInfo : list) { if (packageName.equals(packageInfo.packageName)) { return true; } } return false; } /** * 显示菜单 * @param parent */ public void show(View parent) { //让菜单显示在屏幕的最下方 popupWindow.showAtLocation(parent,Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL,0, 0); // 使其聚集 popupWindow.setFocusable(true); //刷新状态 popupWindow.update(); } /** * 隐藏菜单 */ public void dismiss() { popupWindow.dismiss(); } }
可以用PackageManager
List<ResolveInfo> mApps = new ArrayList<ResolveInfo>(); Intent intent = new Intent(Intent.ACTION_SEND, null); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setType("text/plain"); PackageManager pManager = context.getPackageManager(); mApps = pManager.queryIntentActivities(intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); //遍历可以分享的应用,打印出它们的包名和分享页面的activity名 //还可以读取出他们的图标和其他信息,撸主这里就不一一列出了 for (ResolveInfo resolveInfo : mApps) { System.out.println("AppPackageName: " + resolveInfo.activityInfo.packageName ); System.out.println("ShareActivityName: " + resolveInfo.activityInfo.name ); }
public class ShareAdapter extends BaseAdapter{ private String[] shareNames; private int[] appIcons; private Context context; public ShareAdapter(Context context , int[] appIcons,String[] shareNames){ this.appIcons = appIcons; this.shareNames = shareNames; this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return shareNames.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub Holder holder = null; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.layout_menu_item, parent, false); holder = new Holder(); holder.imgAppIcon = (ImageView) convertView.findViewById(R.id.share_app_icon); holder.txtAppShareName = (TextView) convertView.findViewById(R.id.share_app_name); convertView.setTag(holder); }else { holder = (Holder) convertView.getTag(); } holder.imgAppIcon.setImageResource(appIcons[position]); holder.txtAppShareName.setText(shareNames[position]); return convertView; } final class Holder{ public ImageView imgAppIcon; public TextView txtAppShareName; } }
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); menu = new ShareMenu(this); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (KeyEvent.KEYCODE_MENU == keyCode) { menu.show(new View(this)); } return super.onKeyDown(keyCode, event); } private ShareMenu menu; }
最后的最后,别忘了添加SD卡写入权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />