Android 如何启动具有隐含意图的浏览器服务?
我知道我们可以打开具有隐含意图的浏览器活动:Android 如何启动具有隐含意图的浏览器服务?,android,android-intent,android-service,Android,Android Intent,Android Service,我知道我们可以打开具有隐含意图的浏览器活动: <activity ...> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="andr
<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="http" />
<data android:scheme="https" />
</intent-filter>
</activity>
上面说“android.intent.action.VIEW”是一个活动操作,这就是为什么我不能在服务中使用它
或者是否有任何想法以隐含的意图启动浏览器服务?是的,活动操作必须是活动。此外,您不应该对服务使用意图过滤器。这是可能的,但是它非常非常沮丧。从文件中
“注意:为确保应用程序的安全,请始终使用明确的意图
启动服务时,不要为您的
使用隐式意图启动服务是安全的
危险,因为您无法确定什么服务将响应
用户无法看到哪个服务启动。从
Android 5.0(API级别21),如果您调用
具有隐式意图的bindService()
Intent-imintent=newintent(Intent.ACTION\u视图,Uri.parse(“http:\www.google.com”)
试一试
{
起始触觉(imintent)
}
捕获(ActivityNotFoundException a)
{
}
您可以隐式地将此代码用于启动浏览器活动。
在AndroidManifeat文件中不需要任何更改。经过几天的思考,我找到了一种“黑客方法”来实现这个解决方案
只需将活动的主题设置为android:theme=“@android:style/theme.NoDisplay”
,当一个隐含的意图到达活动开始时,我们只需在它的onCreate()
中做一些事情,例如startService()
,然后只需finish()
此活动即可
整个流程运行速度非常快,体验非常好,看起来棒极了:)
<service ...>
<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="http" />
<data android:scheme="https" />
</intent-filter>
</service>