我尝试监听日历和联系人事件更改,因此我创建了 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/