谁能指导我如何从android浏览器启动我的android应用程序?
当前回答
看这里@JRuns的答案。这个想法是用您的自定义方案创建html,并将其上传到某个地方。然后,如果你点击你的html文件上的自定义链接,你将被重定向到你的应用程序。但是不要忘记设置全名name = "MyApp.Mobile.Droid. "MainActivity”属性到你的目标活动。
其他回答
Xamarin port对菲利克斯的回答
在MainActivity中添加这个(docs: Android.App.IntentFilterAttribute Class):
....
[IntentFilter(new[] {
Intent.ActionView },
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataScheme = "my.special.scheme")
]
public class MainActivity : Activity
{
....
Xamarin将为你在AndroidManifest.xml中添加以下内容:
<activity
android:label="Something"
android:screenOrientation="portrait"
android:theme="@style/MyTheme"
android:name="blah.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="my.special.scheme" />
</intent-filter>
</activity>
为了获得参数(我在MainActivity的OnCreate中测试):
var data = Intent.Data;
if (data != null)
{
var scheme = data.Scheme;
var host = data.Host;
var args = data.PathSegments;
if (args.Count > 0)
{
var first = args[0];
var second = args[1];
...
}
}
据我所知,以上内容可以添加到任何活动中,不仅仅是MainActivity
注:
当用户点击链接,Android操作系统重新启动你的应用程序(杀死前实例,如果有,并运行新的一个),意味着应用程序的MainLauncher活动的OnCreate事件将再次被触发。 这个链接:<a href="my.special. "方案://host/arg1/arg2">,在上面最后的代码段的值将是:
scheme: my.special.scheme
host: host
args: ["arg1", "arg2"]
first: arg1
second: arg2
更新:如果android创建你的应用程序的新实例,你应该添加android:launchMode="singleTask"太。
截至2019年6月15日
我所做的是包括所有四种打开url的可能性。
也就是说,有HTTP / HTTPS, 2有WWW前缀,2没有WWW
通过使用这个,我的应用程序现在自动启动,而不需要我选择浏览器和其他选项。
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" android:host="example.in" />
<data android:scheme="https" android:host="www.example.in" />
<data android:scheme="http" android:host="example.in" />
<data android:scheme="http" android:host="www.example.in" />
</intent-filter>
例如,你有下面的事情:
打开应用程序的链接:http://example.com
应用程序的包名:com.example.mypackage
接下来你需要做的是:
Add an intent filter to your Activity (Can be any activity you want. For more info check the documentation). <activity android:name=".MainActivity"> <intent-filter android:label="@string/filter_title_view_app_from_web"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- Accepts URIs that begin with "http://example.com" --> <data android:host="example.com" android:scheme="http" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> Create a HTML file to test the link or use this methods. Open your Activity directly (just open your Activity, without a choosing dialog). Open this link with browser or your programm (by choosing dialog). Use Mobile Chrome to test That's it.
没有必要在市场上发布应用程序来测试深度链接=)
此外,要获得更多信息,请查看文档和有用的演示文稿。
使用<intent-filter>和<data>元素。例如,要处理所有到twitter.com的链接,你可以把这个放在AndroidManifest.xml中的<activity>中:
<intent-filter>
<data android:scheme="http" android:host="twitter.com"/>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
然后,当用户在浏览器中单击twitter的链接时,将询问他们使用什么应用程序来完成操作:浏览器还是您的应用程序。
当然,如果你想在你的网站和应用程序之间提供紧密的集成,你可以定义你自己的方案:
<intent-filter>
<data android:scheme="my.special.scheme" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
然后,在你的web应用中,你可以添加如下链接:
<a href="my.special.scheme://other/parameters/here">
当用户点击它时,你的应用程序将自动启动(因为它可能是唯一一个可以处理my.special的应用程序)。scheme://类型的uri)。唯一的缺点是,如果用户没有安装应用程序,他们会得到一个讨厌的错误。我不确定有什么办法能查到。
编辑:为了回答你的问题,你可以使用getIntent(). getdata(),它返回一个Uri对象。然后可以使用Uri。*方法来提取所需的数据。例如,假设用户点击了一个到http://twitter.com/status/1234:的链接
Uri data = getIntent().getData();
String scheme = data.getScheme(); // "http"
String host = data.getHost(); // "twitter.com"
List<String> params = data.getPathSegments();
String first = params.get(0); // "status"
String second = params.get(1); // "1234"
你可以在Activity的任何地方执行上述操作,但你可能会想在onCreate()中执行。您还可以使用params.size()来获取Uri中路径段的数量。查看javadoc或android开发人员网站,您可以使用其他Uri方法来提取特定的部分。
嘿,我有办法了。我没有将类别设置为“默认”。此外,我使用的主要活动的意图数据。现在我正在使用一个不同的活动的意图数据。谢谢你的帮助。:)
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。