Native sharing with different Android systems, android sharing
Android shares the sharing feature with Baidu in two ways. One is to use the system's native Activity to bring up a dialog box. The following is a good option, as shown in the right figure, the applications provided above are also relatively complicated. I still remember that a Bug was reported to the monks during a test: I suggest listing frequently used apps, such as QQ, during the sharing.
The other solution makes up for the above two shortcomings. We can use third parties, such as ShareSDK, to customize the interface and decide which courses to share and display on our own. But I am still too lazy to abandon it, because every time I develop a reference, I need to apply for a Key and reference a bunch of packages.
So I still want to find a solution that combines the two advantages above. Until I read the Android native image library, I feel hopeful. The final test results are as follows:
Then we will continue to attract a bigger wave of dislike:
It's still that ugly, it's more ugly than before ...........
But the point is that we can get all the shared application information and share it smoothly. The rest is to make a good look at the interface and filter out the applications that you don't like. In fact, I will not do the rest. Let's talk about how to obtain and share the information.
Because the system uses native sharing, Intent is essential. The following are the differences between Image Sharing and text sharing:
1 Intent intent=new Intent(Intent.ACTION_SEND);2 3 intent.setType("image/*");4 5 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);6 7 intent.putExtra(Intent.EXTRA_STREAM, uri);
1 Intent intent=new Intent(Intent.ACTION_SEND);2 3 intent.setType("text/plain");4 5 intent.putExtra(Intent.EXTRA_TEXT, sharecontentstring);6 7 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Compared with many methods on the Internet, the settings for Flags are different. The rest are the same, and the following are different.
1 List<ResolveInfo> resolveInfo=this.getPackageManager().queryIntentActivities(intent, 0); 2 3 String label; 4 5 Drawable icon; 6 7 ResolveInfo info; 8 9 HashMap<String,Object> item;10 11 datasource.clear();12 13 for(int i=0;i<resolveInfo.size();i++)14 15 {16 17 item=new HashMap<String,Object>();18 19 info=resolveInfo.get(i);20 21 label=info.loadLabel(getPackageManager()).toString();22 23 icon= info.loadIcon(getPackageManager());24 25 if(this.appNameMapping.containsKey(info.activityInfo.applicationInfo.packageName))26 27 item.put("label", this.appNameMapping.get( info.activityInfo.applicationInfo.packageName)+"——"+label);28 29 else30 31 item.put("label", label);32 33 item.put("icon", icon );34 35 item.put("resolve", resolveInfo.get(i));36 37 item.put("intent", intent);38 39 datasource.add(item);40 41 }
Use Context to obtain PackageManager and filter the Activity that matches the Intent. The ResolveInfo is obtained. This ResolveInfo contains the Activity information, the name, and the Icon of the sharing application. You can share the information by using this information, however, you still want to filter out apps that do not need to be displayed in the list by using the application name. In the code above, appNameMapping stores the key-value pairs of the package name and Application name, this is also obtained through a similar method.
1 List<ApplicationInfo> listAppcations = this.getPackageManager() 2 3 .getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES); 4 5 for(ApplicationInfo info :listAppcations) 6 7 { 8 9 appNameMapping.put(info.packageName, info.loadLabel(getPackageManager()));10 11 }
After selecting the application to be shared, the sharing operation will be executed.
1 ResolveInfo resolve=(ResolveInfo) datasource.get(index).get("resolve");2 3 ActivityInfo ai= resolve.activityInfo;4 5 Intent intent=new Intent((Intent) datasource.get(index).get("intent"));6 7 intent.setComponent( new ComponentName(ai.applicationInfo.packageName,ai.name) );8 9 startActivity(intent);
The data is shared.
To sum up a little, this method still has its drawbacks, and it has not been able to share images and text at the same time. If it can be done, it will be better, or the Intent has not been fully understood, this sharing call mainly relies on Intent, and the heap Info class. When I first learned about Android, I still had to understand Intent completely. This method is not encapsulated as a tool or component for the moment. You can also create one by yourself after reading it. Please!