gpt4 book ai didi

android - 从 Android 中的 Intent Chooser 中删除应用程序

转载 作者:行者123 更新时间:2023-11-29 00:03:53 25 4
gpt4 key购买 nike

我有一个可以打开特定 url 的 android 应用程序。所以在我的 list 中,我添加了这样的 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>

当用户点击我的链接(在应用程序外部)时,我的应用程序显示为可以打开 url 的应用程序之一,这很好。

但我的应用程序中有可以以“someurl.com”开头的链接,在这种情况下,我应该从 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)中,当我请求可以处理 url 的 Intent 时,它只是将我的应用程序作为适当的应用程序返回(并且不包括谷歌浏览器等应用程序) ,然后我从 IntentChooser 对话框中删除了我的应用程序,然后就没有什么可供选择了。

我该如何解决这个问题?

最佳答案

有解决方法 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;
}

关于android - 从 Android 中的 Intent Chooser 中删除应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40736931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com