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

时间:2015-03-10 21:12:41

标签: 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>

经过几个小时后,操作系统终止了我的服务,我的应用程序会根据事件的变化进行监听。所以我创建了一些WatchDog:

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

如何解决这个问题,

请帮忙,

2 个答案:

答案 0 :(得分:3)

似乎在分离进程中运行的服务不会监听在同一进程中发送的广播,但会在广播全局时响应。您必须检查实施。我不确定这种行为的确切原因。当您想要与服务进行交互时,最好避免在进程中发送广播。改为发送全球广播。

答案 1 :(得分:-1)

如果Android操作系统杀死了您的服务,请尝试在前台启动它,这样您就可以防止它被杀死。您可以使用Service课程中提供的startForeground(int id, Notification notification)方法。您可以在官方Android API指南中了解Running a Service in the Foreground。此外,您可以在此存储库中使用startForeground()方法找到示例项目:https://github.com/commonsguy/cw-android/