/**
*
* <br>类描述:android分享时,过滤出能发送文本应用程序的列表
* <br>功能详细描述:
*
* @author
*/
public class ShareFilterActivity extends Activity {
//想要过滤出的应用程序列表,前为应用程序的包名,后为应用程序的入口类名
public static final String[][] SHARE_APPS_LIST = new String[][] {
{ "com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI" },
{ "com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity" },
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
//过滤分享,需要过滤掉的直接在循环中continue
private void filterShareWay(String content) {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(
sendIntent, 0);
if (!resInfo.isEmpty()) {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
for (ResolveInfo info : resInfo) {
ActivityInfo activityInfo = info.activityInfo;
if (isInFilterList(activityInfo.packageName, activityInfo.name)) {
Intent targeted = new Intent(Intent.ACTION_SEND);
targeted.setType("text/plain");
targeted.putExtra(Intent.EXTRA_TEXT, content);
targeted.setPackage(activityInfo.packageName);
targeted.setClassName(activityInfo.packageName, activityInfo.name);
targetedShareIntents.add(targeted);
continue;
}
}
if (targetedShareIntents.size() != 0) {
Intent chooserIntent = Intent.createChooser(
targetedShareIntents.remove(0), "share title");
if (chooserIntent == null) {
return;
}
// A Parcelable[] of Intent or LabeledIntent objects as set with
// putExtra(String, Parcelable[]) of additional activities to place
// a the front of the list of choices, when shown to the user with a
// ACTION_CHOOSER.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[] {}));
try {
startActivity(chooserIntent);
} catch (android.content.ActivityNotFoundException ex) {
}
}
}
}
private boolean isInFilterList(String pkgName, String className) {
if (null == pkgName || null == className) {
return false;
}
for (int i = 0; i < SHARE_APPS_LIST.length; i++) {
if (pkgName.contains(SHARE_APPS_LIST[i][0]) && className.contains(SHARE_APPS_LIST[i][1])) {
return true;
}
}
return false;
}
}
该段程序给出了过滤分享途径的方法,但其本身执行不能看到效果,想要看到效果,将Activity的显示过程补齐。
android分享途径过滤的实现方法
最新推荐文章于 2021-12-30 14:42:45 发布