我正在制作一个android应用程序。在IncomingSMS.java中,有一个名为senderNum的字符串。当接收到一条消息并且senderNum字符串是该消息的发送者时,将生成senderNum。我想将senderNum称为另一个类。据说我需要返回字符串的值。但我不知道如何。下面显示的是部分代码。
码:
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]
下面是我要调用
senderNum
字符串的类: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);
}
最佳答案
我对所要询问的内容有些困惑,但我会尽力回答。
我将声明字段private String senderNum
和方法public String getSenderNum(){return senderNum;}
,并删除String
的声明:
String senderNum = phoneNumber;
message = currentMessage.getDisplayMessageBody();
然后在
MainActivity.java
中调用需要的getSenderNum()
。