Ravi
Ravi

Reputation: 960

Android : Using Broadcasting Receiver

I want to read the SMS when it newly comes to my cellphone, I have implemented IncomingSms.java based on this article to check for new SMS as

IncomingSms.java

public class IncomingSms extends BroadcastReceiver {

// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {

    // Retrieves a map of extended data from the intent.
    final Bundle bundle = intent.getExtras();

    try {

        if (bundle != null) {

            final Object[] pdusObj = (Object[]) bundle.get("pdus");

            for (int i = 0; i < pdusObj.length; i++) {

                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                String senderNum = phoneNumber;
                String message = currentMessage.getDisplayMessageBody();

                Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);


                // Show Alert
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(context,
                        "senderNum: "+ senderNum + ", message: " + message, duration);
                toast.show();

            } // end for loop
        } // bundle is null

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" +e);

    }
}

}

but now the problem is I want to call onReceive in my other fragment to but I don't know what to pass as second argument in onReceive method which is demanding Intent ...

Upvotes: 2

Views: 97

Answers (2)

bootiyar
bootiyar

Reputation: 11

if you want call OnReceive ...

private void sendBroadcastToAPI() {
    Intent intent = new Intent();
    String action = "your reciver action";
    Bundle bundle = new Bundle();//save data into bundle
    intent.putExtras(bundle);
    intent.setAction(action);
    sendBroadcast(intent);
}

Upvotes: 1

bootiyar
bootiyar

Reputation: 11

  1. you must add a public interface in your class and create a static instance from it.
  2. in your fragment set interface
  3. like this..!

    public class IncomingSms extends BroadcastReceiver {
    
    public interface IReceiveSMS{
      public void onReceiveData(/*inser your params here*/);
    }
    public static IReceiveSMS receiverSMS;
    // Get the object of SmsManager
    final SmsManager sms = SmsManager.getDefault();
    
    public void onReceive(Context context, Intent intent) {
    
    // Retrieves a map of extended data from the intent.
    final Bundle bundle = intent.getExtras();
    
    try {
    
        if (bundle != null) {
    
        final Object[] pdusObj = (Object[]) bundle.get("pdus");
    
        for (int i = 0; i < pdusObj.length; i++) {
    
            SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
            String phoneNumber = currentMessage.getDisplayOriginatingAddress();
    
            String senderNum = phoneNumber;
            String message = currentMessage.getDisplayMessageBody();
    
            Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);
    
    
            // Show Alert
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context,
                    "senderNum: "+ senderNum + ", message: " + message, duration);
            toast.show();
    
        } // end for loop
      if(receiverSMS != null)
          receiverSMS.onReceiveData(/*inser your params here*/);
    } // bundle is null
    
       } catch (Exception e) {
      Log.e("SmsReceiver", "Exception smsReceiver" +e);
    
        }
      }
    
  4. in your fragment

      IncomingSms.receiverSMS = new IReceiveSMS{
        @Override
        public void onReceiveData(/*inser your params here*/)
        {
         //use params
        }
     }
    
  5. enjoy

Upvotes: 0

Related Questions