简体   繁体   中英

How to call a string from another class

I'm making an android application. in IncomingSMS.java there's this string called the senderNum. senderNum is generated when a message is received and senderNum string is the sender of the message. i want to call senderNum to another class. it is said that i need to return the value of the string. but i don't know how. Shown below are part of the codes.

CODE:

IncomingSMS.java

public class IncomingSms extends BroadcastReceiver {    
private static final byte TARGET_PIN_2 = 0x2;
private String message = "";
private static boolean ledstate = false;

// 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;
                message = currentMessage.getDisplayMessageBody();

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

                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
                toast.show();
             // call led here
                toggleLed();

            }

            // end for loop
        } // bundle is null
    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" + e);

    }
}[/CODE]

below is the class where i want to call the senderNum string:

MainActivity.java

public void sendLOCKED() 
        {
            SmsManager smsManager = SmsManager.getDefault();
                        // i want to call the senderNum below
            smsManager.sendTextMessage("senderNum", null, "LOCKED", null, null);
        }
    public void sendUNLOCKED() 
        {
            SmsManager smsManager = SmsManager.getDefault();
                         // i want to call the senderNum below
            smsManager.sendTextMessage("senderNum", null, "UNLOCKED", null, null);
        }

One way I see is create a new class that extends the class which has the

public void onReceive(Context context, Intent intent)

method. Now in this class declare senderNum as an instance variable. Make it's getter and setter method. Infact you can make it static if it is same across all the instances. Then set this new class as a listener(whichever it is) to the instamce of IncomingSms class. Then you can use getter/setter ot directly reference using class name if static.

I'm a bit confused on what is being asked but I'll try to answer.

I would declare the field private String senderNum and the method public String getSenderNum(){return senderNum;} and remove the declaration of String in:

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

then in MainActivity.java call getSenderNum() where it is needed.

Just declare your string variable as static also after SMSManager to access in everywhere(method) in your IncomingSms class.

   // Get the object of SmsManager
   final SmsManager sms = SmsManager.getDefault();
   public static String senderNum ;

Now where ever(or any other class or method) you to want to get the value of this String just call it using the classname.

    String returnValueOfsenderNum = IncomingSms.senderNum.toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM