我在android清单中设置了默认操作ACTION_VIEW的活动,我想让活动接收我的应用程序的http,https等url类型的方案。见下面的代码
<activity
android:name="com.test.ui.LinkLauncherActivity"
android:label="@string/app_name"
android:excludeFromRecents="true"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https"/>
</intent-filter>
</activity>
现在从我的应用程序中我可以选择共享链接,为此我 想要打开选择意图与选定的应用程序应该只 在选择对话框中列出的排除我自己的应用程序。
目前,当我通过操作查询包管理器时 (ACTION_VIEW)它将返回我的应用程序列表 找到我的包名我只是忽略它并打开选择意图但是 它仍然显示我在自定义共享意图对话框中的应用程序。
是否因为我在上面的代码中将类别定义为清单中的默认值?
让我们分享自定义意图代码
Uri uriUrl = Uri.parse(mlm.getHitUrl());
//get package manager
PackageManager pm = getPackageManager();
Intent viewIntent = new Intent(Intent.ACTION_VIEW,uriUrl);
List<ResolveInfo> resInfo = pm.queryIntentActivities(viewIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>(resInfo.size() - 1);
for (int i = 0; i < resInfo.size(); i++)
{
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
Log.d(getTag(),String.format("PackageName : %2s",packageName));
if (!TextUtils.equals(packageName,"my application packageName")) {
Intent intent = new Intent(Intent.ACTION_VIEW,uriUrl);
intent.setPackage(packageName);
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
// convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);
Intent openInChooser = Intent.createChooser(viewIntent, getResources().getString(R.string.intent_title_open_link));
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
openInChooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(openInChooser);
上面的代码仍然向我展示了我在意图对话框中的应用程序。
答案 0 :(得分:2)
试试这段代码。
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = file.getName().substring(file.getName().indexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
intent.setDataAndType(Uri.fromFile(file),type);
PackageManager packageManager = activity.getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
String packageNameOfAppToHide = "com.test.app";
ArrayList<Intent> targetIntents = new ArrayList<Intent>();
for (ResolveInfo currentInfo : activities) {
String packageName = currentInfo.activityInfo.packageName;
if (packageNameToHide.equals(packageName)) {
Intent targetIntent = new Intent(android.content.Intent.ACTION_VIEW);
targetIntent.setDataAndType(Uri.fromFile(file),type);
targetIntent.setPackage(packageName);
targetIntents.add(targetIntent);
}
}
if(targetIntents.size()>0) {
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "Open file with");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[] {}));
activity.startActivity(chooserIntent);
}
else
Toast.makeText(this, "No app found", Toast.LENGTH_SHORT).show();
com.test.app是您的应用程序的软件包名称。