Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to call string from a different class

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to call string from a different 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.

    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);
     
    		}
    	}

    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);
    		}


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    273
    My Mood
    Bored
    Thanks
    39
    Thanked 76 Times in 64 Posts
    Blog Entries
    4

    Default Re: how to call string from a different class

    'context' is your hook to MainActivity from the IncomingSMS class. Create a private member variable and a setter in MainActivity:

    private String _senderNum;
     
    public void setSenderNum(String senderNum) {
        _senderNum = senderNum;
    }

    then call the setter using the context from IncomingSMS:

    context.setSenderNum(senderNum);

Similar Threads

  1. how to call an object with a string or variable?
    By Zoli in forum Java Theory & Questions
    Replies: 5
    Last Post: September 14th, 2013, 05:32 PM
  2. how to call a string inside a while loop
    By shen_punkz21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2013, 06:25 AM
  3. How do I call a class in a main class
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2011, 03:11 AM
  4. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM