How does Android achieve direct sharing of local images, exchanges, QQ, etc., and direct sharing of images to specified applications,
For example, if you want to share images to and from the app without calling their sdks
Directly paste the following code;
Public void upload image (Bitmap bitmap) {try {Uri uriToImage = Uri. parse (MediaStore. images. media. insertImage (mContext. getContentResolver (), bitmap, null, null); Intent = new Intent (); Intent. setAction (Intent. ACTION_SEND); Specify intent. putExtra (Intent. EXTRA_STREAM, uriToImage); Specify intent. setType ("image/*"); // traverses all applications that support sending images. Find the required application PackageManager packageManager = mContext. getPackageManager (); List <ResolveInfo> resolveInfoList = packageManager. queryIntentActivities (specified intent, PackageManager. GET_INTENT_FILTERS); ComponentName componentName = null; for (int I = 0; I <resolveInfoList. size (); I ++) {if (TextUtils. equals (resolveInfoList. get (I ). activityInfo. packageName, YouNeedAppPackageName) {componentName = new ComponentName (ResolveInfoList. get (I). activityInfo. packageName, resolveInfoList. get (I). activityInfo. name); break ;}// you have installed ** if (null! = ComponentName) {your intent. setComponent (componentName); mContext. startActivity (intent);} else {ContextUtil. getInstance (). showToastMsg ("Install ** first);} catch (Exception e) {ContextUtil. getInstance (). showToastMsg ("Sharing images to ** failed ");}}
The references are as follows:
Set the app default value for android
When a file is opened in Android, the system displays an application selection list for you to choose from. If you set an application as the default value, the selection box is not displayed and the program is called directly.
The setting method is as follows:
PackageManager pm = getPackageManager ();
IntentFilterfilter = new IntentFilter ();
Filter. addCategory ("android. intent. category. DEFAULT ");
ComponentName component = newComponentName ("YourClearAppPakageName", "YourClearAppClassName ");
ComponentName [] components = new ComponentName [] {new ComponentName ("YourPreferAppPakageName", "YourPreferAppClassName"), component };
Pm. clearPackagePreferredActIvities ("YourClearAppPakageName ");
Pm. addPreferredActivity (filter, IntentFilter. MATCH_CATEGORY_EMPTY, components, component );
Included in the modification system default Launhcer, but not the only Launcher
PackageManager pm = getPackageManager();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.MAIN");
filter.addCategory("android.intent.category.HOME");
filter.addCategory("android.intent.category.DEFAULT");
Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(), TestReplaceHomeAppActivity.class.getName()); ComponentName[] components = new ComponentName[] {new ComponentName("com.android.launcher", "com.android.launcher.Launcher"), component};
pm.clearPackagePreferredActivities("com.android.launcher"); pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY, components, component);
Modify the default browser
PackageManager packageManager = context. getPackageManager (); String str1 = "android. intent. category. DEFAULT "; String str2 =" android. intent. category. BROWSABLE "; String str3 =" android. intent. action. VIEW ";
// Set one of the required parameters of the default item. When your operations comply with this filter, IntentFilter = new IntentFilter (str3) is set by default; filter. addCategory (str1); filter. addCategory (str2); filter. addDataScheme ("http"); // sets ActivityComponentName component = new ComponentName (context. getPackageName (), BrowserActivity. class. getName ());
Intent intent = new Intent (str3); intent. addCategory (str2); intent. addCategory (str1); Uri uri = Uri. parse ("http: //"); intent. setDataAndType (uri, null );
// Find all the browser programs currently installed on the mobile phone List resolveInfoList = packageManager. queryIntentActivities (intent, PackageManager. GET_INTENT_FILTERS );
Int size = resolveInfoList. size (); ComponentName [] arrayOfComponentName = newComponentName [size]; for (int I = 0; I <size; I ++) {ActivityInfo activityInfo = resolveInfoList. get (I ). activityInfo; String packageName = activityInfo. packageName; String className = activityInfo. name; // clear the default packageManager. clearPackagePreferredAct Ivities (packageName); ComponentName componentName = newComponentName (packageName, className); parts [I] = componentName;} packageManager. addPreferredActivity (filter, IntentFilter. identifier, identifier, component );