android - BroadcastReceiver 不会使用自定义进程名称触发

标签 android

我尝试监听日历和联系人事件更改,因此我创建了 Service 和 BroadcastReceiver,请参阅 Manifest.xml:

        <service android:name="com.red_folder.phonegap.plugin.backgroundservice.sample.TheService">
            <intent-filter>
                <action android:name="com.red_folder.phonegap.plugin.backgroundservice.sample.TheService" />
            </intent-filter>
        </service>
        <receiver android:name="com.my.the.TheBroadcastReceiver" android:priority="1000" >
            <intent-filter>
                <action android:name="android.intent.action.PROVIDER_CHANGED" />
                <data android:scheme="content" />
                <data android:host="com.android.calendar" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.my.the.BroadcastReceiverContacts" android:priority="1000" >
            <intent-filter>
                <action android:name="android.intent.action.CONTENT_CHANGED" />
                <data android:scheme="content" />
                <data android:host="com.android.calendar" />
            </intent-filter>
        </receiver>

几个小时后,操作系统终止了我的服务,我的应用程序停止监听事件更改。所以我创建了一些看门狗:

            <receiver android:name="com.my.the.service.WatcherReceiver" >
                <intent-filter>
                    <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
                    <action android:name="android.intent.action.BATTERY_OKAY" />
                    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                    <action android:name="android.intent.action.USER_PRESENT" />
                </intent-filter>
            </receiver>

但是当我尝试设置自定义进程名称(android:process=":theService")时,它会停止监听。

        <service android:name="com.red_folder.phonegap.plugin.backgroundservice.sample.TheService" android:process=":theService">
            <intent-filter>
                <action android:name="com.red_folder.phonegap.plugin.backgroundservice.sample.TheService" />
            </intent-filter>
        </service>
        <receiver android:name="com.my.the.TheBroadcastReceiver" android:priority="1000" android:process=":theService">
            <intent-filter>
                <action android:name="android.intent.action.PROVIDER_CHANGED" />
                <data android:scheme="content" />
                <data android:host="com.android.calendar" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.my.the.BroadcastReceiverContacts" android:priority="1000" android:process=":theService">
            <intent-filter>
                <action android:name="android.intent.action.CONTENT_CHANGED" />
                <data android:scheme="content" />
                <data android:host="com.android.calendar" />
            </intent-filter>
        </receiver>

        <receiver android:name="com.my.the.service.WatcherReceiver" android:process=":serviceWatcher">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
                <action android:name="android.intent.action.BATTERY_OKAY" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>

如何解决这个问题

请帮忙,

最佳答案

看来在单独进程中运行的服务不会监听同一进程中发送的广播,但当广播是全局的时会响应。您必须检查实现情况。我不确定这种行为的确切原因。当您想要与服务交互时,最好避免在进程中发送广播。改为发送全局广播。

关于android - BroadcastReceiver 不会使用自定义进程名称触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28974170/

相关文章:

android - 使用 Google Fingerprint API 时,Samsung Galaxy S7 Edge 无法检测到指纹目录更改

java - 替换新方法 dexlib2 失败

android静态库坏ELF号

java - 在手机上看不到我的所有按钮

android - 安装后 OpenCV Android 库错误

android - gmaps 的 MarshMallow 权限

Android,滤色器仅在预览中有效

android - 通过 NFC 标签启动特定 Activity

android - 在 Android 的线程中运行 ProgressDialog

android - 为什么 Corona SDK 的场景隐藏事件没有被触发?以及为什么即使我删除了场景,计时器仍在向控制台打印内容?