1

I'm developing a sms application and i'm not able to receive the "SENT" intent if i'm passing MessageURI as data in the intent for a SMS.No Exception is occurred and the sms is in queued status.OnReceive is not invoked!!!

public class Sms_SendActivity extends Activity {
    PendingIntent sentPI;
    String Sent = "SENT_SMS";
    BroadcastReceiver br;
    Button btnSend;
    Context mcontext;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Uri uri = Uri.parse("Content://sms/2");
        mcontext = getApplicationContext();
        sentPI = PendingIntent.getBroadcast(this,0,new Intent(Sent,uri),0);
        btnSend = (Button)findViewById(R.id.send);
br = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                Log.w("Check","Inside On Receiver");
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(mcontext,"Inside Sms sent", Toast.LENGTH_SHORT).show();
                    Log.w("Check"," URI "+intent.getData().toString());
                    break;

                default:
                    Toast.makeText(mcontext,"failure", Toast.LENGTH_SHORT).show();
                    break;
                }
                unregisterReceiver(br);
            }
        };

        btnSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage("1-212-555-1212", null, "Hi there", sentPI, null);
                Log.w("Check","Sms Queued");
                try {
                    registerReceiver(br, new IntentFilter(Sent,"content://sms"));
                } catch (MalformedMimeTypeException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });

    }
}

Could u pls help me !!!

Thanks

siva
  • 1,429
  • 3
  • 26
  • 47
  • I am doing something similar HERE!!! http://stackoverflow.com/questions/14571564/android-pendingintent-extras-not-received-by-broadcastreceiver/14612215#14612215 – Etienne Lawlor Jan 31 '13 at 07:37

1 Answers1

0

I've a bit modified your code. In my case it works:

public class DynBroadCastSMSActivity extends Activity {


private static final String TAG = DynBroadCastSMSActivity.class.getSimpleName();

PendingIntent sentPI;
String Sent = "SMS_SENT";
Button btnSend;
Context mContext;

BroadcastReceiver br = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.w("Check", "Inside On Receiver");
    }
};

@Override
protected void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter();
    filter.addAction(Sent);
    filter.addDataScheme("mycontent");
    registerReceiver(br, filter);
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(br);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Uri uri = Uri.parse("mycontent://sms");
    Intent intent = new Intent(Sent);
    intent.setData(uri);
    mContext = getApplicationContext();
    sentPI = PendingIntent.getBroadcast(this, 0, intent, 0);
    btnSend = (Button) findViewById(R.id.send);



    btnSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage("5554", null, "Hi there", sentPI,
                    null);
            Log.w("Check", "Sms Queued");
        }
    });

}

}

The problem was the following. Android does not recognize data scheme called "content". If I call it "mycontent" everything works. You can play with this code to catch further details.

Yury
  • 20,618
  • 7
  • 58
  • 86
  • Thanks @Yury It works fine.But i'm accessing a sms. so the uri will be of the form "content://sms/".But if i'm replacing "mycontent" with "content".OnReceive is not invoked.Can u clarify ? Thanks in advance – siva Dec 30 '11 at 17:51
  • I do not know how to explain this. I think in other case you should specify the type of your data. But try to look for the precise answer. – Yury Dec 30 '11 at 18:36
  • `intentFilter.addDataScheme("content"); intentFilter.addDataType("vnd.android-dir/mms-sms");` – siva Dec 30 '11 at 20:30