• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Reading SMS then Passing to String attribute

Hello, I am trying to write a program that reads the SMS and then Pass it into a String Variable in the same Class, I am having programs.
This is what I have done so far :

In the SmsReceiver class:

public class SmsReceiver extends BroadcastReceiver()
{

public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
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);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();

String senderNum = phoneNumber;
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);

}
set_sms();

//---display the new SMS message---
Intent intentHome = new Intent(context,MainActivity.class);
intentHome.putExtra("msgContent", message);

intentHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentHome);



}

The message can be called from another class which is MainActivity with this code :

Bundle extras = getIntent().getExtras();
if (extras != null) {
messageContent = extras.getString("msgContent");
}



Indeed , I want to use the message in the first class and deal with it. I have tried but all the programs shows me an error in my app
 
Back
Top Bottom