如何以隐式意图启动浏览器服务?

How to start a browser service with implicit intent?

我知道我们可以打开浏览器 activity 隐式意图:

<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>

但是现在我想打开一个浏览器服务来在我们点击链接时在后台加载网页,所以我就这样做了:

<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>

但它不起作用,应用选择器没有出现,浏览器服务也没有运行。

Intent | Android Developers 它说 "android.intent.action.VIEW" 是一个 Activity 操作 ,那是主电源我不能将它与服务一起使用吗?

或者是否有任何想法以隐式意图启动浏览器服务?

是的,activity 动作必须是 activity。此外,您不应将意图过滤器与服务一起使用。这是可能的,但非常、非常气馁。来自文档

"Caution: To ensure your app is secure, always use an explicit intent when starting a Service and do not declare intent filters for your services. Using an implicit intent to start a service is a security hazard because you cannot be certain what service will respond to the intent, and the user cannot see which service starts. Beginning with Android 5.0 (API level 21), the system throws an exception if you call bindService() with an implicit intent."

Intent imintent=new Intent(Intent.ACTION_VIEW,Uri.parse("http:\www.google.com"));

尝试

{

startActivity(imintent);

} 赶上(ActivityNotFoundException 一个)

{ }

您可以使用此代码隐式启动浏览器 activity。 不需要在 Androidmanifeast 文件中进行任何更改。

经过几天的思考,我找到了 "hack way" 来实施这个解决方案。

只需将 activity 的主题设置为 android:theme="@android:style/Theme.NoDisplay",当隐式意图到达 activity 开始时,我们只需在 onCreate() 中做一些事情,例如 startService() 然后只是 finish() 这个 activity.

整个过程运行非常快,体验很好,看起来很棒:)