編輯:關於Android編程
因為公司的需求,最近一直在做分享這一塊的功能。大概有這樣幾種思路:
1.使用Intent調用andoird原生的分享功能;
2.使用第三方的sdk,比如ShareSdk或者友盟;
3.去對應的平台下載jar包,參考官方設計文檔寫出自己的分享demo,但這種一般也比較復雜,尤其搞不懂qq和微信一家公司的,為什麼微信那麼麻煩。
不廢話了,直接上代碼:
一. 新建ShareUtil.java類
import java.io.File; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.net.Uri; import android.text.TextUtils; import android.widget.Toast; public class ShareUtil { private Context context; public ShareUtil(Context context) { this.context = context; } public static final String WEIXIN_PACKAGE_NAME = ""; public static final String QQ_PACKAGE_NAME = ""; // public static final String ; /** * 分享文字 * @param packageName * @param content * @param title * @param subject */ public void shareText(String packageName,String className,String content,String title,String subject){ Intent intent =new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setType("text/plain"); // if(null != className && null != packageName && !TextUtils.isEmpty(className) && !TextUtils.isEmpty(packageName)){ // // }else { // if(null != packageName && !TextUtils.isEmpty(packageName)){ // intent.setPackage(packageName); // } // } if(stringCheck(className) && stringCheck(packageName)){ ComponentName componentName = new ComponentName(packageName, className); intent.setComponent(componentName); }else if(stringCheck(packageName)){ intent.setPackage(packageName); } intent.putExtra(Intent.EXTRA_TEXT, content); if(null != title && !TextUtils.isEmpty(title)){ intent.putExtra(Intent.EXTRA_TITLE, title); } if(null != subject && !TextUtils.isEmpty(subject)){ intent.putExtra(Intent.EXTRA_SUBJECT, subject); } intent.putExtra(Intent.EXTRA_TITLE, title); Intent chooserIntent = Intent.createChooser(intent, "分享到:"); context.startActivity(chooserIntent); } /** * 分享網頁 */ public void shareUrl(String packageName,String className,String content,String title,String subject){ Intent intent =new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setType("text/plain"); // if(null != className && null != packageName && !TextUtils.isEmpty(className) && !TextUtils.isEmpty(packageName)){ // // }else { // if(null != packageName && !TextUtils.isEmpty(packageName)){ // intent.setPackage(packageName); // } // } if(stringCheck(className) && stringCheck(packageName)){ ComponentName componentName = new ComponentName(packageName, className); intent.setComponent(componentName); }else if(stringCheck(packageName)){ intent.setPackage(packageName); } intent.putExtra(Intent.EXTRA_TEXT, content); if(null != title && !TextUtils.isEmpty(title)){ intent.putExtra(Intent.EXTRA_TITLE, title); } if(null != subject && !TextUtils.isEmpty(subject)){ intent.putExtra(Intent.EXTRA_SUBJECT, subject); } intent.putExtra(Intent.EXTRA_TITLE, title); Intent chooserIntent = Intent.createChooser(intent, "分享到:"); context.startActivity(chooserIntent); } /** * 分享圖片 */ public void shareImg(String packageName,String className,File file){ if(file.exists()){ Uri uri = Uri.fromFile(file); Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setType("image/*"); if(stringCheck(packageName) && stringCheck(className)){ intent.setComponent(new ComponentName(packageName, className)); }else if (stringCheck(packageName)) { intent.setPackage(packageName); } intent.putExtra(Intent.EXTRA_STREAM, uri); Intent chooserIntent = Intent.createChooser(intent, "分享到:"); context.startActivity(chooserIntent); }else { Toast.makeText(context, "文件不存在", 1000).show(); } } /** * 分享音樂 */ public void shareAudio(String packageName,String className,File file){ if(file.exists()){ Uri uri = Uri.fromFile(file); Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setType("audio/*"); if(stringCheck(packageName) && stringCheck(className)){ intent.setComponent(new ComponentName(packageName, className)); }else if (stringCheck(packageName)) { intent.setPackage(packageName); } intent.putExtra(Intent.EXTRA_STREAM, uri); Intent chooserIntent = Intent.createChooser(intent, "分享到:"); context.startActivity(chooserIntent); }else { Toast.makeText(context, "文件不存在", 1000).show(); } } /** * 分享視頻 */ public void shareVideo(String packageName,String className,File file){ setIntent("video/*", packageName, className, file); } public void setIntent(String type,String packageName,String className,File file){ if(file.exists()){ Uri uri = Uri.fromFile(file); Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setType(type); if(stringCheck(packageName) && stringCheck(className)){ intent.setComponent(new ComponentName(packageName, className)); }else if (stringCheck(packageName)) { intent.setPackage(packageName); } intent.putExtra(Intent.EXTRA_STREAM, uri); Intent chooserIntent = Intent.createChooser(intent, "分享到:"); context.startActivity(chooserIntent); }else { Toast.makeText(context, "文件不存在", 1000).show(); } } /** * 分享多張圖片和文字至朋友圈 * @param title * @param packageName * @param className * @param file 圖片文件 */ public void shareImgToWXCircle(String title,String packageName,String className, File file){ if(file.exists()){ Uri uri = Uri.fromFile(file); Intent intent = new Intent(); ComponentName comp = new ComponentName(packageName, className); intent.setComponent(comp); intent.setAction(Intent.ACTION_SEND); intent.setType("image/*"); intent.putExtra(Intent.EXTRA_STREAM, uri); intent.putExtra("Kdescription", title); context.startActivity(intent); }else{ Toast.makeText(context, "文件不存在", Toast.LENGTH_LONG).show(); } } /** * 是否安裝分享app * @param packageName */ public boolean checkInstall(String packageName){ try { context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); return true; } catch (NameNotFoundException e) { e.printStackTrace(); Toast.makeText(context, "請先安裝應用app", 1500).show(); return false; } } /** * 跳轉官方安裝網址 */ public void toInstallWebView(String url){ Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse(url)); context.startActivity(intent); } public static boolean stringCheck(String str){ if(null != str && !TextUtils.isEmpty(str)){ return true; }else { return false; } } }
import java.io.File; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { Button btnQQ; Button btnWX; Button btnMore; Button btnWxFriendText; Button btnQQFriendText; Button btnWxFriendImg; Button btnQQFriendImg; Button btnWxFriendAudio; Button btnQQFriendAduio; Button btnWxFriendVideo; Button btnQQFriendVideo; ShareUtil shareUtil; private Button btn_wxCircle_img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnQQ = (Button) findViewById(R.id.btn_qq); btnWX = (Button) findViewById(R.id.btn_wx); btnMore = (Button) findViewById(R.id.btn_more); btnWxFriendText = (Button) findViewById(R.id.btn_wxFriend); btnQQFriendText = (Button) findViewById(R.id.btn_qqFriend); btnWxFriendImg = (Button) findViewById(R.id.btn_wxFriend_img); btnQQFriendImg = (Button) findViewById(R.id.btn_qqFriend_img); btnWxFriendAudio = (Button) findViewById(R.id.btn_wxFriend_audio); btnQQFriendAduio = (Button) findViewById(R.id.btn_qqFriend_audio); btnWxFriendVideo = (Button) findViewById(R.id.btn_wxFriend_video); btnQQFriendVideo = (Button) findViewById(R.id.btn_qqFriend_video); btn_wxCircle_img = (Button) findViewById(R.id.btn_wxCircle_img); btnQQ.setOnClickListener(this); btnWX.setOnClickListener(this); btnMore.setOnClickListener(this); btnWxFriendText.setOnClickListener(this); btnQQFriendText.setOnClickListener(this); btnWxFriendImg.setOnClickListener(this); btnQQFriendImg.setOnClickListener(this); btnWxFriendAudio.setOnClickListener(this); btnQQFriendAduio.setOnClickListener(this); btnWxFriendVideo.setOnClickListener(this); btnQQFriendVideo.setOnClickListener(this); btn_wxCircle_img.setOnClickListener(this); shareUtil = new ShareUtil(this); } @Override public void onClick(View v) { String testImgPath = "/storage/emulated/legacy/display-client/picture/my.png"; String testImagePath = Environment.getExternalStorageDirectory() + "/img.jpg"; String testAudioPath = Environment.getExternalStorageDirectory() + "/audio.mp3"; String testVideoPath = Environment.getExternalStorageDirectory() + "/video.mp4"; File file = new File(testImgPath); File fileImage = new File(testImagePath); File fileAudio = new File(testAudioPath); File fileVideo = new File(testVideoPath); switch (v.getId()) { // qq&文字 case R.id.btn_qq: shareUtil.shareText("com.tencent.mobileqq", null, "這是一條分享信息", "分享標題", "分享主題"); break; // 微信&文字 case R.id.btn_wx: shareUtil.shareText("com.tencent.mm", null, "這是一條分享信息", "分享標題", "分享主題"); break; // 所有&文字 case R.id.btn_more: shareUtil.shareText(null, null, "這是一條分享信息", "分享標題", "分享主題"); break; // 微信朋友&文字 case R.id.btn_wxFriend: if (shareUtil.checkInstall("com.tencent.mm")) { shareUtil.shareText("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI", "http://www.aiipu.com/", "分享標題", "分享主題"); } else { shareUtil.toInstallWebView("http://weixin.qq.com/download"); } break; // qq朋友&文字 case R.id.btn_qqFriend: if (shareUtil.checkInstall("com.tencent.mobileqq")) { shareUtil.shareText("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity", "http://www.aiipu.com/", "分享標題", "分享主題"); } else { shareUtil.toInstallWebView("http://im.qq.com/mobileqq/"); } break; // 微信朋友&圖片 case R.id.btn_wxFriend_img: shareUtil.shareImg("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI", fileImage); break; // qq朋友&圖片 case R.id.btn_qqFriend_img: shareUtil.shareImg("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity", fileImage); break; case R.id.btn_wxFriend_audio: shareUtil.shareAudio("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI", fileAudio); break; case R.id.btn_qqFriend_audio: shareUtil.shareAudio("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity", fileAudio); break; case R.id.btn_wxFriend_video: shareUtil.shareVideo("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI", fileVideo); break; case R.id.btn_qqFriend_video: shareUtil.shareVideo("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity", fileVideo); break; case R.id.btn_wxCircle_img: shareUtil.shareImgToWXCircle("狗狗圖片", "com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI", fileImage); break; } } }
三.布局文件activity_main.xml
其中微信的分享只能分享文字和圖片,不能單獨分享圖片或者文字。
Android aapt自動打包工具概念在Android.mk中有LOCAL_AAPT_FLAGS配置項,在gradle中也有aaptOptions,那麼aapt到底是干
一、測試性能Demo 首先給大家一個性能測試的Demo,看看性能如何。 Demo.apk 二、相關操作CRUD Entity: import android.an
先給大家這是下效果圖:谷歌提供的v4包,ViewPager在布局文件中,先添加<android.support.v4.view.ViewPager/>控件,這
前面的博文介紹了Activity,可能學的還不太爽哦,所以接下來學習android的Intent,它是android的信使,可以進行各個組件之間進行通信了。我依然在前面的