Android基于Chrome意图的URI是如何打开的
从中,我了解可以使用以下URI打开应用程序Android基于Chrome意图的URI是如何打开的,android,uri,deep-linking,Android,Uri,Deep Linking,从中,我了解可以使用以下URI打开应用程序 intent: HOST/URI-path // Optional host #Intent; package=\[string\]; action=\[string\]; category=\[string\]; component=\[string\]; scheme=\[string\]; end; 我想知道是否可以从我的应用程序打开此
intent:
HOST/URI-path // Optional host
#Intent;
package=\[string\];
action=\[string\];
category=\[string\];
component=\[string\];
scheme=\[string\];
end;
我想知道是否可以从我的应用程序打开此URI
示例URI
intent:
//qr/json/%7B%22u%22%3A%22https%3A%2F%2Fprivacybydesign.foundation%2Fbackend%2Firma%2Fsession%2FvsRjkZF2B2H17sBWmVZe%22%2C%22irmaqr%22%3A%22disclosing%22%7D
#Intent;
package=org.irmacard.cardemu;
scheme=cardemu;
l.timestamp=1620907855707;
S.browser_fallback_url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dorg.irmacard.cardemu
;end
因为它看起来像一个普通的URI,我想我可以这样打开它:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("%EXAMPLE_URI%"));
startActivity(intent);
这给了我一个ActivityNotFoundException
。我错过了什么
原因:android.content.ActivityNotFoundException:未找到可处理Intent{act=android.Intent.action.VIEW dat的活动=intent://qr/json/{“u”:”https://privacybydesign.foundation/backend/irma/session/vsRjkZF2B2H17sBWmVZe“,“irmaqr”:“披露”}pkg=org.irmacard.cardemu(有附加条款)}
以下是您可以尝试的几件事: 1。确保要启动的应用程序实现了正确的目的。
<application android:label="@string/app_name">
...
<activity android:name=".PleaseStartThisActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
...
</application>
Intent intent = new Intent(android.content.Intent.ACTION_VIEW); //OK.
// Intent intent = getIntent(); //Not OK.
// If package name and activity are known.
intent.setComponent(new ComponentName("org.irmacard.cardemu", "org.irmacard.cardemu.PleaseStartThisActivity"));
// Else
// intent = new Intent(android.content.Intent.ACTION_GET_CONTENT);
// intent.setDataAndType(Uri.parse("file://" + filePath), "text/plain");
startActivity(intent);
// Or this:
// startActivity(android.content.Intent.createChooser(intent, null));
3。把其他事情都安排好。
<application android:label="@string/app_name">
...
<activity android:name=".PleaseStartThisActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
...
</application>
Intent intent = new Intent(android.content.Intent.ACTION_VIEW); //OK.
// Intent intent = getIntent(); //Not OK.
// If package name and activity are known.
intent.setComponent(new ComponentName("org.irmacard.cardemu", "org.irmacard.cardemu.PleaseStartThisActivity"));
// Else
// intent = new Intent(android.content.Intent.ACTION_GET_CONTENT);
// intent.setDataAndType(Uri.parse("file://" + filePath), "text/plain");
startActivity(intent);
// Or this:
// startActivity(android.content.Intent.createChooser(intent, null));
4。设置或添加标志可能会有所帮助
intent.setFlags(
android.content.Intent.FLAG_ACTIVITY_FORWARD_RESULT |
android.content.Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP |
android.content.Intent.FLAG_INCLUDE_STOPPED_PACKAGES |
android.content.Intent.FLAG_RECEIVER_FOREGROUND |
android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION |
android.content.Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
android.content.Intent.FLAG_GRANT_PREFIX_URI_PERMISSION |
android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION |
android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
5。现在您可以安全地开始活动了。
<application android:label="@string/app_name">
...
<activity android:name=".PleaseStartThisActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
...
</application>
Intent intent = new Intent(android.content.Intent.ACTION_VIEW); //OK.
// Intent intent = getIntent(); //Not OK.
// If package name and activity are known.
intent.setComponent(new ComponentName("org.irmacard.cardemu", "org.irmacard.cardemu.PleaseStartThisActivity"));
// Else
// intent = new Intent(android.content.Intent.ACTION_GET_CONTENT);
// intent.setDataAndType(Uri.parse("file://" + filePath), "text/plain");
startActivity(intent);
// Or this:
// startActivity(android.content.Intent.createChooser(intent, null));
编辑
* intent.getDataString() - To get intent data (You may have to parse it to get the package name).
* getPackageManager().queryIntentActivities() - To check available activities for that package like the following example:
private final String getActivity(final String packageName、final Intent Intent、final Bundle、final String mimeType){
最终串活动;
int activityLen=0;
final int packageLen=packageName.length();
if(bundle!=null&(activity=bundle.getString(“activity”)!=null){
activityLen=activity.length();
}否则{
活动=空;
}
最终列表列表=getPackageManager().QueryInputActivities(intent,PackageManager.GET_RESOLVED_FILTER);
对于(resolveinfori:list){
最终意图过滤器过滤器=ri.filter;
最后一个字符串activityName=ri.activityInfo.name;
//确保该活动对于包有效且存在。
//不区分大小写
if(activity!=null&&activity.regionMatches(true,0,activityName,0,activityLen)
&&packageName.regionMatches(true,0,ri.activityInfo.packageName,0,packageLen)){
返回activityName;
}else if(filter==null){
Toast.makeText(此“错误:没有可用的操作来处理此文件”,1).show();
返回null;
//仅当用户未指定活动时自动搜索,否则返回null。不区分大小写
}else if(activity==null&&filter.hasAction(intent.getAction())&&filter.hasDataType(mimeType)){
if(ri.activityInfo.packageName.regionMatches(true,0,packageName,0,packageLen)){
返回ri.activityInfo.name;
}
}//否则继续验证下一个活动
}
如果(活动==null){
Toast.makeText(此“错误:没有可用的活动可打开此文件”,1).show();
}否则{
makeText(此“错误:未找到此类活动”,1).show();
}
返回null;
}
中描述的“intent:”语法用于从网页启动应用程序,Chrome将处理href
,并检索参数以通过Android intent启动应用程序
您提供的示例URI的方案是intent://
,默认情况下不进行处理
如果要处理intent://
URI,需要
如果您想从Android应用程序打开网页,可以使用ACTION_VIEW操作并在意向数据中指定web URL
public void openWebPage(String url) {
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
// example
// openWebPage("https://privacybydesign.foundation/backend/irma/session/vsRjkZF2B2H17sBWmVZe")
有关您在何处调用startActivity(intent)的更多信息?从my Main Activity,chrome将使用该意图识别您的应用程序,并在安装后启动该应用程序。在内部,你不能用它来启动活动。如果chrome能做到,我应该也能做到,对吗?我不这么认为。根据你所期望的结果,可能有不同的处理方法。你能给我更多关于你如何使用它以及为什么使用它的上下文吗?我很确定Chrome并没有宣布所有可能的活动。这不是Chrome,而是应用本身。应用程序是否在其
Manifest.xml
中有android.intent.action.VIEW
作为活动?还要确保传递正确的mime类型。我注意到如果mime:*/*
,可能会调用android软件包安装程序。但是chrome如何处理它呢?chrome使用WebView加载网页,并通过WebViewClient捕获href
链接。例如: