閱讀提醒
本文只是關于如何實現Android系統分享,并非第三方SDK實現方法
Android開發時通過startActivity發送action為Intent.ACTION_SEND的Intent即很容易就可以實現系統分享功能,舉個簡單例子看看:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Here is the Shared text.");
//切記需要使用Intent.createChooser,否則會出現別樣的應用選擇框,您可以試試
shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box");
startActivity(shareIntent);
從例子中,可以發現實現系統分享主要由三部分Action、Extras和Type組成。首先將Intent的cation設置為Intent.ACTION_SEND,其次根據分享的內容設置不同的Type,然后根據不同的社交平臺設置相關Extras,最后將Intent發送出去即可完成系統分享。
以下列舉幾種分享類型:
- 分享文本內容
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Here is the Shared text.");
//切記需要使用Intent.createChooser,否則會出現別樣的應用選擇框,您可以試試
shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box");
startActivity(shareIntent);
- 分享圖片
//將mipmap中圖片轉換成Uri
Uri imgUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" +R.mipmap.ic_launcher);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
//其中imgUri為圖片的標識符
shareIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
shareIntent.setType("image/*");
//切記需要使用Intent.createChooser,否則會出現別樣的應用選擇框,您可以試試
shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box");
startActivity(shareIntent);
- 分享多張圖片
Uri imgUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" +R.mipmap.ic_launcher);
ArrayList<Uri> imgUris = new ArrayList<Uri>();
imgUris.add(imgUri); //其中imgUri1為第一張圖片的標識符
imgUris.add(imgUri); //其中imgUri2為第二張圖片的標識符
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
//其中fileUri為文件的標識符
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imgUris);
shareIntent.setType("image/*");
//切記需要使用Intent.createChooser,否則會出現別樣的應用選擇框,您可以試試
shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box");
startActivity(shareIntent);
- 分享郵件
Uri imgUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" +R.mipmap.ic_launcher);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
//其中imgUri為圖片的標識符
shareIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
shareIntent.setType("image/*");
//設置郵件主題
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Here is the email subject");
//郵件內容
shareIntent.putExtra(Intent.EXTRA_TEXT, "Here is the email content");
//切記需要使用Intent.createChooser,否則會出現別樣的應用選擇框,您可以試試
shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box");
startActivity(shareIntent);
//EXTRA_BCC:存放郵件密送人地址的字符串數組。
//EXTRA_CC:存放郵件抄送人地址的字符串數組。
//EXTRA_EMAIL:存放郵件地址的字符串數組
問題拓展
1.如何將自己的應用能夠顯示在系統分享的應用選擇框中?
根據以上介紹,我們可以在應用清單文件中使用<intent-filter>來完成;
<activity
android:name=".ShareHandleActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<!-- 根據分享類型的需要進行修改 -->
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
2.如何監聽在應用選擇框中,選擇了那個應用?
需要采用BroadcastReceiver來實現:(該方法在部分手機上可以實現,并且需要API Level大于等于22)
- 創建BroadcastReceiver
public class ShareDoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
for(String key:intent.getExtras().keySet()){
try {
ComponentName componentInfo = (ComponentName) intent.getExtras().get(key);
PackageManager packageManager = context.getPackageManager();
String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(componentInfo.getPackageName(), PackageManager.GET_META_DATA));
Log.i("Selected", appName);
} catch (Exception e) {
}
}
}
}
- AndroidManifest中注冊廣播
<receiver android:name=".ShareDoneReceiver"/>
- 獲取廣播進行分享
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Here is the Shared text.");
Intent receiver = new Intent(this, ShareDoneReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
//切記需要使用Intent.createChooser,否則會出現別樣的應用選擇框,您可以試試
shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box", pendingIntent.getIntentSender());
startActivity(shareIntent);
3.如何為制定應用設置分享type?
List<Intent> targetIntents = new ArrayList<Intent>();
//獲取所有支持ACTION_SEND的應用
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
PackageManager pm = getPackageManager();
List<ResolveInfo> resInfos = pm.queryIntentActivities(shareIntent, 0);
//對目標引用進行判斷
if(resInfos != null && resInfos.size() > 0){
for(ResolveInfo info:resInfos){
Intent target = new Intent(Intent.ACTION_SEND);
//需要知道制定應用的package name
if ("com.tencent.mm".equals(info.activityInfo.packageName)) {
target.setType("text/plain");
}else{
target.setType("image/*");
//其中imgUri為圖片的標識符
target.putExtra(Intent.EXTRA_STREAM, imgUri);
}
target.putExtra(Intent.EXTRA_TEXT, "Here is the email content");
target.setComponent(new ComponentName( info.activityInfo.packageName, info.activityInfo.name));
targetIntents.add(target);
}
}
//創建應用選擇框
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
4.如何只顯示指定的應用?
List<Intent> targetIntents = new ArrayList<Intent>();
//獲取所有支持ACTION_SEND的應用
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
PackageManager pm =getPackageManager();
List<ResolveInfo> resInfos = pm.queryIntentActivities(shareIntent, 0);
//對目標引用進行判斷
if(resInfos != null && resInfos.size() > 0){
for(ResolveInfo info:resInfos){
if("com.netease.mobimail".equals(info.activityInfo.packageName)
|| "com.google.android.talk".equals(info.activityInfo.packageName)){
Intent target = new Intent(Intent.ACTION_SEND);
target.setType("text/plain");
target.putExtra(Intent.EXTRA_TEXT, "Here is the email content");
target.setComponent(new ComponentName( info.activityInfo.packageName, info.activityInfo.name));
targetIntents.add(target);
}
}
}
//創建應用選擇框
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);