【发布时间】:2012-01-04 14:05:19
【问题描述】:
我需要在单击通过电子邮件获得的超链接时唤醒我的应用程序。
有什么想法吗?请帮忙。
提前致谢。
【问题讨论】:
-
您自己提供这个超链接吗?即,这是您自己生成的电子邮件吗?
-
是的。我会将邮件发送给用户。
我需要在单击通过电子邮件获得的超链接时唤醒我的应用程序。
有什么想法吗?请帮忙。
提前致谢。
【问题讨论】:
这可以通过使用自定义 URI 方案(如由 Market 应用处理的 market: 网址)或使用带有 intent: 方案的自定义操作来完成。
在这两种情况下,您都应该创建一个在用户单击您的链接时启动的活动。
我们先来看第一种情况:
首先在清单中声明活动:
<activity android:name="LinkHandler">
<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="SACPK" android:host="www.anyhost.com" />
</intent-filter>
</activity>
在这种情况下,链接应该类似于SACPK://www.anyhost.com/anything-goes-here。
您的活动将收到意图中的整个链接,因此您可以根据查询参数或路径对其进行处理并决定下一步做什么:
public class LinkHandler extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Uri uri = getIntent().getData();
// this is the URI containing your link, process it
}
}
这次的链接应该是下面的格式:
intent:#Intent;action=com.sacpk.CUSTOM_ACTION;end
intent-filter 应该包含一个相应的动作,你将在你的活动中检查:
<intent-filter>
<action android:name="com.sacpk.CUSTOM_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
在你的onCreate 方法中:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ("com.sacpk.CUSTOM_ACTION".equals(getIntent().getAction()) {
// then you really know you got here from the link
}
}
此方法的缺点是您不会根据您的意图获取任何数据。
整个答案基于 commonsware 的精彩书籍The Busy Coder's Guide to Advanced Android Development。
【讨论】: