RegisterReceiver inside IntentService, SmsManager tracking

I am trying to track when sms is sending progress using a class that extends "IntentService".

The message is sent and I can see it on another device, the problem is that the "BroadcastReceiver" won't fire the onReceive method.

While sendBroadcast for my mainActivity works for RegisterReceiver, inside it it won't work.

My service class:

public class SmsReciveService extends IntentService {



public SmsReciveService() {
    super("SmsReciveService");
}


public static final String NOTIFICATION = "com.example.Send";

public static ArrayList<String> phoneNumbers = new ArrayList<String>();
public static String messageToSent="";  
public static final String hello = "";
public static int numberOfRecives;
public static int numberOfRecivesCurrent;
public static int delay;
public static int sentCounter =0;
public static int notSentCounter =0;

Intent intent2 = new Intent(NOTIFICATION);

@Override
protected void onHandleIntent(Intent intent){

  phoneNumbers = (ArrayList<String>) intent.getSerializableExtra("members");
  messageToSent = intent.getStringExtra("messageToSent");
  sendSMS();
}


public void sendSMS()  {



  String smsSent = "SEND";

  final ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
  final SmsManager sm = SmsManager.getDefault();
  final ArrayList<String> parts =sm.divideMessage(messageToSent);
  final int numParts = parts.size();

  for (int j = 0; j < numParts; j++) {
  sentIntents.add(PendingIntent.getBroadcast(this, 0, new Intent(smsSent), 0));
  }

  numberOfRecives=numParts;
  delay = numParts*2500;

  Intent intent2 = new Intent(NOTIFICATION);
  intent2.putExtra(hello, "sending,Number of parts :" +  Integer.toString(numParts) );
  sendBroadcast(intent2);

  registerReceiver(new BroadcastReceiver() {

    @Override
      public void onReceive(Context arg0, Intent arg1) {

        numberOfRecivesCurrent+=1;

        Intent intent2 = new Intent(NOTIFICATION);
        intent2.putExtra(hello, "test");
        sendBroadcast(intent2);


          if(numberOfRecivesCurrent==numberOfRecives){

          switch (getResultCode()){     
          case Activity.RESULT_OK:  break; 
          case SmsManager.RESULT_ERROR_GENERIC_FAILURE:break;
          case SmsManager.RESULT_ERROR_NO_SERVICE:break;    
          case SmsManager.RESULT_ERROR_NULL_PDU:break;
          case SmsManager.RESULT_ERROR_RADIO_OFF:break;
          }

        numberOfRecives+=numParts;

          }
      }
   }, new IntentFilter(smsSent));



  for(int i=0; i<phoneNumbers.size(); i++){  
      Handler hand = new Handler();
      final int index=i;

      hand.postDelayed(new Runnable() {
          @Override
          public void run() {

              String phone = phoneNumbers.get(index).split(",")[1]; 
              sm.sendMultipartTextMessage(phone,null, parts, sentIntents, null);

          }
      }, delay*i);
  }
}

      

Repeater BroadcastReceiver inside the main class:

private BroadcastReceiver receiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
      String string = intent.getStringExtra(SmsReciveService.hello);
      Toast.makeText(Send.this,string,Toast.LENGTH_LONG).show();
  }
};

protected void onPause() {
    super.onPause();
    unregisterReceiver(receiver);
}

protected void onResume() {
super.onResume();
registerReceiver(receiver, new IntentFilter(SmsReciveService.NOTIFICATION));
}

      

What am I doing wrong? Please help, welcome.

+3


source to share


2 answers


Done with this tutorial:

Example: Communication Between Activity and Service Using Messages



now the user can go to the network or answer the call or whatever., the track and sms are running in the background, when the user comes back, he sees the changes on the internet.

0


source


Your receiver must be registered in the manifest, not through registerReceiver()

. Once onHandleIntent()

finished, yours IntentService

will be destroyed, taking your dynamically registered receivers.



+2


source







All Articles