3

I am trying to track after sms sending progress by using class that extends 'IntentService'.

The message is sent and i see it on other device , the problem is that the 'BroadcastReceiver' wont run the onReceive method .

While sendBroadcast to my mainActivity is working befor RegisterReceiver , inside it it wont 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);
  }
}

BroadcastReceiver reciver inside 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 i am doing wrong ? Please help,greeting.

Stav Bodik
  • 2,018
  • 3
  • 18
  • 25

2 Answers2

2

Your receiver should be registered in the manifest, not via registerReceiver(). Once onHandleIntent() ends, your IntentService is destroyed, taking away your dynamically-registered receivers.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Done with this tutorial :

Example: Communication between Activity and Service using Messaging

now user can go surf the net, or answere a call or whatever . . . the track and sending sms's are working in the background , when user comes back he see the changes online .

Community
  • 1
  • 1
Stav Bodik
  • 2,018
  • 3
  • 18
  • 25