preface
Recently, I am working on an information-related APP, which needs a sharing function. The project is not big. If I use the official SDK, I still need to review it. Chanced upon zhihu sharing interface did a good job, get me this project is appropriate, check the information on the Internet, using the share system at BottomSheetDialogFragment combined with the effect of the function can be done it.
Zhihu Sharing Interface:
Self-completed renderings:
Layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/fragment_share_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:text="@string/shareTo"
android:textColor="@color/black"
android:gravity="center_vertical"
android:textSize="16sp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/fragment_share_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
/>
</LinearLayout>
Copy the code
data
To get a collection of apps on your phone that can receive the data we share, it’s simple:
public static List<ResolveInfo> getShareList(Context context) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, 0);
return list;
}
Copy the code
If you only want to show specific apps, or if you want to show some apps earlier, you can filter or filter by APP package name here. For example, if you want to advance Tencent’s APP, you can do this:
// Put wechat and QQ first
Collections.sort(list, new Comparator<ResolveInfo>() {
@Override
public int compare(ResolveInfo resolveInfo, ResolveInfo t1) {
ActivityInfo activityInfo1 = resolveInfo.activityInfo;
ActivityInfo activityInfo2 = t1.activityInfo;
if (activityInfo1.packageName.contains("com.tencent.")
&& !activityInfo2.packageName.contains("com.tencent.")) {
return -1;
} else if(! activityInfo1.packageName.contains("com.tencent.")
&& activityInfo2.packageName.contains("com.tencent.")) {
return 1;
}
return 0; }});Copy the code
Then we can easily get the names and ICONS of these apps.
Drawable icon = list.get(i).loadIcon(context.getPackageManager());
String label = list.get(i).loadLabel(context.getPackageManager()).toString();
Copy the code
Interface code
The use of a new Fragment succession BottomSheetDialogFragment, BottomSheetDialogFragment is very simple, interface code to write and in ordinary fragments, Then we simply call its show()/dismiss() to make it show or close. The complete shareFragment code:
public class ShareFragment extends BottomSheetDialogFragment {
private List<ResolveInfo> mShareResolveInfoList;
private List<ShareItem> mShareList;
private Context mContext;
private static String mTitle;
private static String mUrl;
public static ShareFragment getInstance(String title, String url) {
mTitle = title;
mUrl = url;
return new ShareFragment();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mContext = getContext();
View view = inflater.inflate(R.layout.fragment_share, container, false);
initData();
initViews(view);
return view;
}
private void initViews(View view) {
RecyclerView recyclerView = view.findViewById(R.id.fragment_share_recyclerView);
recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
ShareRecyclerViewAdapter adapter = new ShareRecyclerViewAdapter(mShareList, mContext);
adapter.setOnClickShareItemListener(new ShareRecyclerViewAdapter.OnClickShareItemListener() {
@Override
public void OnClick(int position) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "I found a good article on Xunbo:" + "\n" + mTitle + "\n" + mUrl);
intent.setType("text/plain"); ActivityInfo activityInfo = mShareResolveInfoList.get(position).activityInfo; intent.setClassName(activityInfo.packageName, activityInfo.name); startActivity(intent); dismiss(); }}); recyclerView.setAdapter(adapter); }private void initData(a) {
mShareList = new ArrayList<>();
mShareResolveInfoList = ShareUtil.getShareList(mContext);
for (int i = 0; i < mShareResolveInfoList.size(); i++) {
ShareItem item = newShareItem(); item.setIcon(mShareResolveInfoList.get(i).loadIcon(mContext.getPackageManager())); item.setLabel(mShareResolveInfoList.get(i).loadLabel(mContext.getPackageManager()).toString()); mShareList.add(item); }}}Copy the code
If you want to share something else like an image, change the intent. SetType (“text/plain”) to intent.setType(“image/ JPEG “) and post the shared object in the intent.
Encapsulate the shared display as a utility class method that can be easily invoked from any interface
public static void share(FragmentManager fragmentManager, String title, int id) {
String url = "xxx";
ShareFragment.getInstance(title, url).show(fragmentManager, "dialog");
}
Copy the code
The last
If there are any mistakes or improvements in this article, feel free to leave me a comment in the comments section.