我最终使用了Url Scheme,对于此任务,可以在此处找到更多信息:
android custom url scheme..?
代码是,在我的主 Activity 的清单文件中,我提供了以下意图过滤器:
<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="com.myhost" />
</intent-filter>
在活动中我会这样做:
Intent intent = getIntent();
String value = null;
if (intent.getData() != null)
{
value = intent.getData().getQueryParameter("server");
}
if (value != null)
{
Log.d(TAG, "with scheme value: "+ value);
if (value.equals("my_test_server_address"))
{
Toast.makeText(this, "Server set to Test" , Toast.LENGTH_LONG).show();
}
else if (value.equals("my_production_server_address"))
{
Toast.makeText(this, "Server set to Production" , Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "Server set to Address: "+ value , Toast.LENGTH_LONG).show();
}
Consts.BASE_URL = Uri.parse(value);
}
else
{
Log.d(TAG, "value was null");
}
最后,要使用此意图过滤器启动您的应用程序,您需要创建一个 HTML 文件,其中包含以下代码:
<a href="myapp://com.myhost?server=my_test_server_address">test</a>
<a href="myapp://com.myhost?server=my_production_server_address">production</a>