我有一个可以打开特定网址的Android应用。所以在我的Manifest中我添加了如下所示的intent-filter部分:
<intent-filter>
<data andriod:host="someurl.com"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
当用户点击我的链接(应用外部)时,我的应用会显示为可以打开网址的应用之一,并且没问题。
但是我的应用程序内部有链接,可以用&#34; someurl.com&#34;在这种情况下,我应该从IntentChooser对话框中删除我的应用程序。我这样做如下:
public Intent generateCustomChooserIntent(Intent prototype) throws Exception {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(prototype, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo resolveInfo : resInfo) {
// do not include my app in intent chooser dialog
if (resolveInfo.activityInfo == null || resolveInfo.activityInfo.packageName.equals(getPackageName())) {
continue;
}
// add Intent to intent chooser dialog
Intent targetedShareIntent = (Intent) prototype.clone();
targetedShareIntent.setPackage(resolveInfo.activityInfo.packageName);
targetedShareIntent.setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
targetedShareIntents.add(targetedShareIntent);
}
if (!targetedShareIntents.isEmpty()) {
// pass new Intent to create no chooser in first row
Intent chooserIntent = Intent.createChooser(targetedShareIntents.get(0), getString(R.string.open_link_with));
targetedShareIntents.remove(0);
// pass extra intent chooser
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[targetedShareIntents.size()]));
return chooserIntent;
}
}
// there is no appropriate intent to run
throw new Exception();
}
它也很好。
但在某些Android设备(如三星A5 2016)中,当我要求可以处理网址的意图时,它只是将我的应用程序作为适当的应用程序返回(并且不包含谷歌浏览器等应用程序),然后我从IntentChooser对话框中删除我的应用程序,然后没有什么可供选择。
我该如何解决这个问题?
答案 0 :(得分:1)
您可以使用解决方法here
public Intent generateCustomChooserIntent(Context context, String url) throws Exception {
Uri fakeUri = Uri.parse("https://www.google.com");
Uri realUri = Uri.parse(url);
Intent shareIntent = new Intent(Intent.ACTION_VIEW, fakeUri);
List<ResolveInfo> resInfo;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
resInfo = context.getPackageManager().queryIntentActivities(
shareIntent, PackageManager.MATCH_ALL);
} else {
resInfo = context.getPackageManager().queryIntentActivities(
shareIntent, 0);
}
if (!resInfo.isEmpty()) {
List<Intent> targetedShareIntents = removeCurrentApp(context, realUri, resInfo);
if (!targetedShareIntents.isEmpty()) {
// pass new Intent to create no chooser in first row
Intent chooserIntent = Intent.createChooser(
targetedShareIntents.get(0), context.getString(R.string.open_link_with));
targetedShareIntents.remove(0);
// pass extra intent chooser
chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[targetedShareIntents.size()]));
return chooserIntent;
}
}
// there is no appropriate intent to run
throw new Exception();
}
@NonNull
private List<Intent> removeCurrentApp(Context context, Uri realUri, List<ResolveInfo> resInfo) {
List<Intent> targetedShareIntents = new ArrayList<>();
String currentPackageName = context.getPackageName();
for (ResolveInfo resolveInfo : resInfo) {
// do not include my app in intent chooser dialog
if (resolveInfo.activityInfo == null) {
continue;
}
String packageName = resolveInfo.activityInfo.packageName;
if (currentPackageName.equalsIgnoreCase(packageName)) {
continue;
}
Intent intent = new Intent(Intent.ACTION_VIEW, realUri);
intent.setClassName(
resolveInfo.activityInfo.applicationInfo.packageName,
resolveInfo.activityInfo.name);
intent.setPackage(packageName);
targetedShareIntents.add(intent);
}
return targetedShareIntents;
}