Intent Service does not start when called from a broadcast receiver

I am working on an application in which, when I receive a message, I need to start the service. But my Intent service is not working. That's what I'm doing:

Broadcast Receiver:

public void onReceive(Context context, Intent intent) { this.con=context; Toast.makeText(context,"Broadcast received", Toast.LENGTH_LONG).show(); Bundle bundle = intent.getExtras(); Object[] messages = (Object[])bundle.get("pdus"); SmsMessage[] sms = new SmsMessage[messages.length]; for(int i=0;i<messages.length;i++) { sms[i] = SmsMessage.createFromPdu((byte[]) messages[i]); } String smsFrom = sms[0].getDisplayOriginatingAddress().toString(); Intent in= new Intent(context, ResponseService.class); in.putExtra("sender",smsFrom); context.startService(in); } 

IntentService:

 public class ResponseService extends IntentService { public ResponseService(String name) { super(name); // TODO Auto-generated constructor stub } @Override protected void onHandleIntent(Intent intent) { // TODO Auto-generated method stub Log.d("Notify", "In Response Service"); getCurrentLocation(); } } 

Nothing happens in the log file. can someone please help me understand the problem? Thanks in advance.

Update I already declared both the broadcast receiver and the service in manifest.xml as follows:

 <receiver android:name=".BroadCastReceiver" > <intent-filter android:priority="10000" > <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> <service android:name=".ResponseService" /> 

Also, I tried to run the application again, and after a while the application closed the application giving java.lang.instantiationexception Can someone please help me with this problem?

0
source share
4 answers

My first question is: will your broadcast receiver work? Is a toast displayed in your broadcast receiver? Second question: did you declare your intention in the manifest? You can do it as follows:

 <service android:name="yourPackage.ResponseService" > 
+1
source

The exception, java.lang.InstantiationException really solved my problem. I did not have a public constructor with a null argument. I just changed

 public ResponseService(String name) { super(name); // TODO Auto-generated constructor stub } 

IN:

 public ResponseService() { super("ResponseService"); // TODO Auto-generated constructor stub } 

And now it works very well. Hope this helps others too.

+1
source

You should use WakefulBroadcastReceiver:

A WakefulBroadcastReceiver is a special type of broadcast receiver that creates and manages partial track locks for your application. It disables the processing of SMS messages with your ResponseService (IntentService), ensuring that the device does not enter sleep mode upon transition, so your Intent Service will work correctly.

 public class YourReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Explicitly specify that ResponseService will handle the intent. ComponentName comp = new ComponentName(context.getPackageName(), ResponseService.class.getName()); this.con=context; Toast.makeText(context,"Broadcast received", Toast.LENGTH_LONG).show(); Bundle bundle = intent.getExtras(); Object[] messages = (Object[])bundle.get("pdus"); SmsMessage[] sms = new SmsMessage[messages.length]; for(int i=0;i<messages.length;i++) { sms[i] = SmsMessage.createFromPdu((byte[]) messages[i]); } String smsFrom = sms[0].getDisplayOriginatingAddress().toString(); Intent in= new Intent(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 look like this:

  @Override protected void onHandleIntent(Intent intent) { Bundle extras = intent.getExtras(); if (!extras.isEmpty()) { //Do something } // Release the wake lock provided by the WakefulBroadcastReceiver. YourReceiver.completeWakefulIntent(intent); } 
0
source

I already warned about this, but you can easily forget to declare it by your wakefulbroadcastreceiver in your manifest.xml.

0
source

Source: https://habr.com/ru/post/974636/


All Articles