Intent Service Do Not Get Started When Called From A Broadcast Receiver
Solution 1:
My first question would be: Is your broadcast receiver working? Is the toast in your broadcast receiver being displayed? Second question: Did you declare your intent service in manifest? You can do so the following way:
<serviceandroid:name="yourPackage.ResponseService" >
Solution 2:
The exception, java.lang.InstantiationException really solved my problem. I was missing a zero argument public constructor. I just changed
public ResponseService(String name)
{
super(name);
// TODO Auto-generated constructor stub
}
Into:
public ResponseService()
{
super("ResponseService");
// TODO Auto-generated constructor stub
}
And now it's working really well. Hope it might help others as well.
Solution 3:
You should use a WakefulBroadcastReceiver:
A WakefulBroadcastReceiver is a special type of broadcast receiver that takes care of creating and managing a partial wake lock for your app. It passes off the work of processing the SMS to a your ResponseService (IntentService), while ensuring that the device does not go back to sleep in the transition, so your Intent Service will run correctly.
publicclassYourReceiverextendsWakefulBroadcastReceiver {
@OverridepublicvoidonReceive(Context context, Intent intent) {
// Explicitly specify that ResponseService will handle the intent.ComponentNamecomp=newComponentName(context.getPackageName(),
ResponseService.class.getName());
this.con=context;
Toast.makeText(context,"Broadcast received", Toast.LENGTH_LONG).show();
Bundlebundle= intent.getExtras();
Object[] messages = (Object[])bundle.get("pdus");
SmsMessage[] sms = newSmsMessage[messages.length];
for(int i=0;i<messages.length;i++)
{
sms[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
}
StringsmsFrom= sms[0].getDisplayOriginatingAddress().toString();
Intent in= newIntent(context, ResponseService.class);
in.putExtra("sender",smsFrom)
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
And your IntentService will be like:
@OverrideprotectedvoidonHandleIntent(Intent intent) {
Bundleextras= intent.getExtras();
if (!extras.isEmpty()) {
//Do something
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
YourReceiver.completeWakefulIntent(intent);
}
Solution 4:
I have done alarms before but you can easily forget to declare it along your wakefulbroadcastreceiver in your manifest.xml.
Post a Comment for "Intent Service Do Not Get Started When Called From A Broadcast Receiver"