简体   繁体   English

如何从另一个类调用字符串

[英]How to call a string from another class

I'm making an android application. 我正在制作一个Android应用程序。 in IncomingSMS.java there's this string called the senderNum. 在IncomingSMS.java中有一个名为senderNum的字符串。 senderNum is generated when a message is received and senderNum string is the sender of the message. 收到邮件时生成senderNum,senderNum字符串是邮件的发件人。 i want to call senderNum to another class. 我想将senderNum调用到另一个类。 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 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: 下面是我想调用senderNum字符串的类:

MainActivity.java 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. 现在在这个类中声明senderNum作为实例变量。 Make it's getter and setter method. 使它成为getter和setter方法。 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. 然后将此新类设置为IncomingSms类的instamce的侦听器(无论哪个)。 Then you can use getter/setter ot directly reference using class name if static. 然后,如果是静态的,你可以使用getter / setter ot直接引用类名。

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: 我将声明字段private String senderNum和方法public String getSenderNum(){return senderNum;}并删除String中的String声明:

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

then in MainActivity.java call getSenderNum() where it is needed. 然后在MainActivity.java调用getSenderNum() ,在那里需要它。

Just declare your string variable as static also after SMSManager to access in everywhere(method) in your IncomingSms class. 在SMSManager访问IncomingSms类中的任何地方(方法)之后,只需将您的字符串变量声明为静态。

   // 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的值的任何地方(或任何其他类或方法)只需使用类名称来调用它。

    String returnValueOfsenderNum = IncomingSms.senderNum.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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