【问题标题】:How to start a browser service with implicit intent?如何以隐式意图启动浏览器服务?
【发布时间】:2015-03-22 08:27:39
【问题描述】:

我知道我们可以打开一个带有隐含意图的浏览器活动:

<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 Action,我不能将它与Service 一起使用?

或者有什么想法以隐式意图启动浏览器服务?

【问题讨论】:

    标签: android android-intent android-service


    【解决方案1】:

    经过几天的思考,我找到了一种“破解方法”来实施这个解决方案。

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

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

    【讨论】:

      【解决方案2】:

      是的,活动动作必须是活动。此外,您不应将意图过滤器与服务一起使用。它可能,但它非常非常不鼓励。来自文档

      “警告:为确保您的应用安全,请始终使用明确的意图 启动服务时,不要为您的服务声明意图过滤器 服务。使用隐式意图启动服务是一种安全措施 危险,因为您无法确定什么服务会响应 意图,并且用户看不到哪个服务启动。以。。。开始 Android 5.0 (API level 21),调用会抛出异常 具有隐含意图的 bindService()。”

      【讨论】:

        【解决方案3】:

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

        试试

        {

        开始活动(意图);

        } catch(ActivityNotFoundException a)

        { }

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

        【讨论】:

          猜你喜欢
          • 2023-03-22
          • 2011-06-19
          • 2016-05-24
          • 1970-01-01
          • 2012-03-10
          • 1970-01-01
          • 1970-01-01
          • 2012-05-17
          • 2015-12-20
          相关资源
          最近更新 更多