Android app 使用scheme
时间: 2024-03-15 19:22:32 浏览: 104
Android app 使用scheme可以让其他应用程序或者网页链接打开你的应用程序,并且可以携带参数,实现应用程序之间的跳转和交互。以下是Android app 使用scheme的步骤:
1. 在AndroidManifest.xml文件中注册scheme。例如,你可以在<activity>标签中添加一个<intent-filter>标签,并且在<intent-filter>标签中添加一个<data>标签,如下所示:
```xml
<activity android:name=".MainActivity">
<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="myapp" android:host="openactivity" />
</intent-filter>
</activity>
```
2. 在应用程序中处理scheme。在你的MainActivity中,你可以通过以下代码来处理scheme:
```java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
if (data != null) {
String host = data.getHost();
if (host.equals("openactivity")) {
//处理scheme参数,打开指定的Activity
}
}
}
```
3. 在其他应用程序或者网页中使用scheme来打开你的应用程序。例如,你可以在浏览器中输入“myapp://openactivity”来打开你的应用程序并且跳转到指定的Activity。
注意事项:
1. 必须在<intent-filter>标签中添加<category android:name="android.intent.category.BROWSABLE" />,否则无法在浏览器中打开应用程序。
2. scheme参数区分大小写。
3. 如果你的应用程序中有多个Activity需要处理scheme,需要在<intent-filter>标签中分别添加<data>标签,每个<data>标签的scheme和host必须不同。
阅读全文
相关推荐















