编辑:
经过测试和测试,我发现如果您的方案包含大写字符,浏览器将无法启动它。您的方案应该只包含小写字符!
另外请注意,铬项目的错误 459156 仍然不允许您直接启动 url,您应该将用户引用到包含此 JavaScript 代码的网页:
<!DOCTYPE html>
<html>
<body>
<script language="javascript">
window.location = 'customscheme://customHost/49FYTJTF00';
</script>
</body>
</html>
登陆此页面的用户将自动收到活动选择器对话框提示或直接发送到您的活动。
要试试看,打开安卓浏览器去下面的url,把上面的sn-p复制粘贴到编辑器里:
http://www.w3schools.com/html/tryit.asp?filename=tryhtml_intro
显示浏览器 + JavaScript 打开 Activity 的 Gif
原帖
我试用了您的自定义 URI,它适用于 Android 5.0
但您应该注意以下两个错误/问题:
-
Bug 459156 of the Chromium project 这基本上意味着从 Android 浏览器启动自定义方案不起作用,除非使用 JavaScript 应用 URL。有关解决方法,请参阅此 StackOverflow 帖子:OAuth and custom scheme result in a "ERR_UNKNOWN_URL_SCHEME" in Chrome
- Bug 80971: URI with custom scheme are not clickable in SMS Text (Linkify?)
我的方法
我创建了两个独立的活动,一个作为意图接收器,另一个作为意图启动器。启动活动有一个 EditText 可以输入完整的 URI 和一个按钮来启动输入的 URI。
Main.java onClick(启动活动)
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(input.getText().toString()));
try {
startActivity(intent);
} catch(ActivityNotFoundException e){
Toast.makeText(this, "Auww!! No Activity can open this URI!", Toast.LENGTH_SHORT).show();
}
}
manifest.xml(仅限活动)
注意<data android:pathPattern=".*"/> 部分。这部分很重要,因此主机之后的任何内容都将被视为有效。
<activity
android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".UriActivity"
android:label="@string/app_name">
<!--
This intent-filter will open any URI with the following forms:
customscheme://customHost/49FYTJTF00
customscheme://customHost
https://www.google.com/something/something
http://www.google.com/
http://google.com/something
-->
<intent-filter>
<data android:scheme="https"/>
<data android:scheme="http"/>
<data android:host="google.com"/>
<data android:host="www.google.com"/>
<data android:scheme="customscheme"/>
<data android:host="customHost"/>
<data android:pathPattern=".*"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
UriActivity.java(接收活动)
public class UriActivity extends ActionBarActivity {
private TextView tvText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uri);
tvText = (TextView) findViewById(R.id.text);
setTextFromIntent();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setTextFromIntent();
}
private void setTextFromIntent(){
StringBuilder text = new StringBuilder();
Uri data = getIntent().getData();
if(data != null){
text.append("Path:\n");
text.append(data.getPath());
text.append("\n\nScheme:\n");
text.append(data.getScheme());
text.append("\n\nHost:\n");
text.append(data.getHost());
text.append("\n\nPath segments:\n");
text.append(Arrays.toString(data.getPathSegments().toArray()));
} else {
text.append("Uri is null");
}
tvText.setText(text);
}
}
截图:
测试项目下载:
如果您不想自己尝试,我为该项目创建了download。