1

Hi I am developing an android SMS app where I am using a broadcast receiver inside an activity refering the below link,

SMS Delivery Report in Android

I have tried this code

public class Myapp extends Activity 
{
  BroadcastReceiver sendBroadcastReceiver= new sentReceiver();

    protected void onCreate(Bundle savedInstanceState) 
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activtiy_main);


    }

   public void onClick(View v)
  {
    if(v.getId()==sendmsg.getId())
    {

        sendSMS(phoneNumber,message);

    }

 @Override
  protected void onDestroy() 
  {
    super.onDestroy();
    try
    {
        unregisterReceiver(sendBroadcastReceiver);
    }
    catch (Exception e)
    {
         e.printStackTrace();
    }
  }
     //unregistering Receiver even in onPause()

  private void sendSMS(String phoneNumber, String message)
  {
    String SENT = "SMS_SENT";
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
    registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, null);

  }
  class sentReceiver extends BroadcastReceiver 
  {
    @Override
    public void onReceive(Context context, Intent arg1)

    {
        switch (getResultCode()) 

        {
        case Activity.RESULT_OK:
            Toast.makeText(getBaseContext(), "sms_sent", Toast.LENGTH_SHORT).show();
             startActivity(new Intent(Myapp.this, Sm.class));
             break;
        case SmsManager.RESULT_ERROR_NO_SERVICE:
            Toast.makeText(getBaseContext(), "No service",Toast.LENGTH_SHORT).show();
            Log.e("No service","");
            break;
        }
     }                  

   }

}

But I am getting warnings as

java.lang.IllegalArgumentException: Receiver not registered: com.example.Myapp$Receiver@40593ab8

Not sure How I need to register the receiver in manifest.Please suggest.Thanks!

Community
  • 1
  • 1
sanjana
  • 641
  • 2
  • 15
  • 34
  • Try to use anonymous class for Receiver. http://www.vineetdhanawat.com/blog/2012/04/how-to-use-broadcast-receiver-in-android-send-and-receive-sms/ is a good example. – Pankaj Kumar Sep 03 '13 at 09:06
  • Do not initialize broadcast reciever globally. "BroadcastReceiver sendBroadcastReceiver= new sentReceiver();" Initialize this in send message method and check in on destroy if sendBroadcastReceiver is null then don't execute unregister code. – DcodeChef Sep 03 '13 at 09:09

2 Answers2

0

1. You are registering your receiver programmatically with

registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));

So there is no need to add it to the Manifest aswell.

2. You have a custom BroadcastReceiver-class called "sentReceiver", so you should use this objecttype when you instantiate it.

So you need to change

BroadcastReceiver sendBroadcastReceiver= new sentReceiver();

to

SentReceiver sendBroadcastReceiver = new sentReceiver();

3. I think you are calling your stuff in the wrong order. So that the receiver is not ready listening when it should be.

bofredo
  • 2,338
  • 6
  • 31
  • 49
0

You need to instantiate sendBroadcastReceiver in the oncreate method of your activity. This will allow for "re-registration" after the ondestroy method is called and the receiver is unregistered by your code. And as Bofredo noted, use your custom calss to instantiate.

rmsanger
  • 71
  • 1
  • 2
  • 11