程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> intent-在 android 中分享照片時如何過濾應用程序

intent-在 android 中分享照片時如何過濾應用程序

編輯:編程綜合問答
在 android 中分享照片時如何過濾應用程序

我使用下面的代碼通過其它的程序來來分享照片,然後打印它們的包名:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(FilePath));
PackageManager packageManager = this.getPackageManager();
List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(sharingIntent, PackageManager.MATCH_DEFAULT_ONLY);
int i = 0;
while(i < resolveInfo.size()) {
    System.out.println(i + "  " + resolveInfo.get(i).activityInfo.packageName);
    i++;
}
startActivity(Intent.createChooser(sharingIntent, "Share");

獲取了下面的9個包名,可以分享照片:

06-29 16:55:22.460: I/System.out(13020): 0  com.amazon.kindle
06-29 16:55:22.460: I/System.out(13020): 1  com.android.bluetooth
06-29 16:55:22.460: I/System.out(13020): 2  com.google.android.apps.uploader
06-29 16:55:22.460: I/System.out(13020): 3  com.ecareme.asuswebstorage
06-29 16:55:22.460: I/System.out(13020): 4  com.google.android.talk
06-29 16:55:22.460: I/System.out(13020): 5  com.google.android.gm
06-29 16:55:22.460: I/System.out(13020): 6  com.aripollak.picturemap
06-29 16:55:22.460: I/System.out(13020): 7  com.instagram.android
06-29 16:55:22.460: I/System.out(13020): 8  com.facebook.katana

如果當我選擇應用程序時想過濾一些其它的程序,例如,我想從app列表中過濾 facebook 程序,如何實現?

最佳回答:


    Intent it = new Intent(Intent.ACTION_SEND);
    it.setType("image/jpeg");
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(it, 0);
    if (!resInfo.isEmpty()) {
        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        for (ResolveInfo info : resInfo) {
            Intent targeted = new Intent(Intent.ACTION_SEND);
            targeted.setType("image/jpeg";
            ActivityInfo activityInfo = info.activityInfo;

            // judgments : activityInfo.packageName, activityInfo.name, etc.
            if (activityInfo.packageName.contains("facebook") ||activityInfo.name.contains("facebook")) {
                continue;
            }
            targeted.setPackage(activityInfo.packageName);
            targetedShareIntents.add(targeted);
        }

        Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
        if (chooserIntent == null) {
            return;
        }
        try {
chooserIntent .putExtra(Intent.EXTRA_STREAM, Uri.parse(FilePath));
            startActivity(chooserIntent);
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(this, "Can't find share component to share", Toast.LENGTH_SHORT).show();
        }
    }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved