例如:要在app中直接分享图片到微信、来往等,而不调用他们的sdk
直接贴代码如下;
public void shareImage(Bitmap bitmap) { try { Uri uriToImage = Uri.parse(MediaStore.Images.Media.insertImage( mContext.getContentResolver(), bitmap, null, null)); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage); shareIntent.setType("image/*"); // 遍历所有支持发送图片的应用。找到需要的应用 PackageManager packageManager = mContext.getPackageManager(); List<ResolveInfo> resolveInfoList = packageManager .queryIntentActivities(shareIntent, 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; } } // 已安装** if (null != componentName) { shareIntent.setComponent(componentName); mContext.startActivity(shareIntent); } else { ContextUtil.getInstance().showToastMsg("请先安装**"); } } catch (Exception e) { ContextUtil.getInstance().showToastMsg("分享图片到**失败"); } }
参考的文章如下:
android设置应用程序默认值
Android中有时打开一个文件,系统会弹出一个应用程序选择列表供你选择.如果你把某个应用程序设为默认值,则不会弹出选择框,而会直接调用该程序.
设置方法如下:
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);
列入修改系统默认Launhcer但不是唯一的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);
PackageManager packageManager =context.getPackageManager();
String str1 = "android.intent.category.DEFAULT";
String str2 = "android.intent.category.BROWSABLE";
String str3 = "android.intent.action.VIEW";
//设置默认项的必须参数之一,用户的操作符合该过滤器时,默认设置起效
IntentFilter filter = new IntentFilter(str3);
filter.addCategory(str1);
filter.addCategory(str2);
filter.addDataScheme("http");
//设置浏览页面用的Activity
ComponentName 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);
//找出手机当前安装的所有浏览器程序
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;
//清除之前的默认设置
packageManager.clearPackagePreferredActivities(packageName);
ComponentName componentName = newComponentName(packageName,
className);
arrayOfComponentName[i] = componentName;
}
packageManager.addPreferredActivity(filter,
IntentFilter.MATCH_CATEGORY_SCHEME,arrayOfComponentName,
component);