Broadcast Receiver issue
Ok, so I have an app that needs to receive incoming SMS, and send out an SMS to the sender. This I can set up fine. The problem is, I only want the Broadcast Receiver to receive when the service is started. I declared the receiver class within the service. I destroy the reference (and unregister the receiver) in onDestroy. While the app compiles, a开发者_StackOverflow社区nd runs, the broadcast receiver never runs. If I delcare it in the manifest, it works fine, but never stops. The receiver just keeps waking up and processing.
You can try to implement this code:
<receiver android:name=".mystuff" android:enabled="false">
on
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(
new ComponentName(context, mystuff.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
off
... PackageManager.COMPONENT_ENABLED_STATE_DISABLED ...
One thing to mention is that you need to declare the Receiver file in your manifest for sure. This actually registers your service with the phone. According to API docs
A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.
So what i have done is i defined my onReceive(Context, Intent) within the Service and register it. Hope this helps.
精彩评论