gpt4 book ai didi

android - 为什么我的广播接收器无法识别我的字符串 - 发送短信代码

转载 作者:行者123 更新时间:2023-11-30 01:13:52 25 4
gpt4 key购买 nike

我希望 phoneNo 在我的 onReceive 方法范围内被识别。我需要做什么 ?我以为这样声明就可以了,但没有快乐:

public class MainActivity extends Activity {
Button sendBtn;
EditText txtphoneNo;
String phoneNo;

// register a broadcast receiver
private BroadcastReceiver receiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

sendBtn = (Button) findViewById(R.id.btnSendSMS);
txtphoneNo = (EditText) findViewById(R.id.editText);
phoneNo = txtphoneNo.getText().toString(); etc etc....

目前我的代码如下所示。当广播接收器检测到收到的短信时,我想打印到 logcat:

        System.out.println(origNumber);
System.out.println(phoneNo);

但是我不能让 phoneNo 在整个 Activity 中被识别,只能在 sendSMSmessage 的范围内。

public class MainActivity extends Activity {
Button sendBtn;
EditText txtphoneNo;
EditText txtMessage;

// register a broadcast receiver
private BroadcastReceiver receiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

sendBtn = (Button) findViewById(R.id.btnSendSMS);
txtphoneNo = (EditText) findViewById(R.id.editText);
txtMessage = (EditText) findViewById(R.id.editText2);

sendBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
});

IntentFilter filter = new IntentFilter();
// the thing we're looking out for is received SMSs
filter.addAction("android.provider.Telephony.SMS_RECEIVED");

receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)

{
//do something based on the intent's action

Bundle extras = intent.getExtras();

if (extras == null)
return;

Object[] pdus = (Object[]) extras.get("pdus");
SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);
String origNumber = msg.getOriginatingAddress();

System.out.println(origNumber);
// I want to print phoneNo here!!!!
// System.out.println(phoneNo);

}
};
registerReceiver(receiver, filter);
}



protected void sendSMSMessage() {
Log.i("Send SMS", "");
String phoneNo = txtphoneNo.getText().toString();
String message = txtMessage.getText().toString();

try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
}

catch (Exception e) {
Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}

@Override
protected void onDestroy() {
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
super.onDestroy();
}

}

最佳答案

问题是您将 phoneNo 声明为 sendSMSMessage() 的局部变量,因此无法在该方法之外访问它。

您应该将其声明为实例变量。

像这样:

public class MainActivity extends Activity {
Button sendBtn;
EditText txtphoneNo;
EditText txtMessage;

String phoneNo;

private BroadcastReceiver receiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

sendBtn = (Button) findViewById(R.id.btnSendSMS);
txtphoneNo = (EditText) findViewById(R.id.editText);
txtMessage = (EditText) findViewById(R.id.editText2);

sendBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
});

IntentFilter filter = new IntentFilter();
// the thing we're looking out for is received SMSs
filter.addAction("android.provider.Telephony.SMS_RECEIVED");

receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)

{
Bundle extras = intent.getExtras();

if (extras == null)
return;

Object[] pdus = (Object[]) extras.get("pdus");
SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);
String origNumber = msg.getOriginatingAddress();

System.out.println(origNumber);
if(phoneNo != null) {
System.out.println(phoneNo);
}

}
};
registerReceiver(receiver, filter);
}



protected void sendSMSMessage() {
Log.i("Send SMS", "");
phoneNo = txtphoneNo.getText().toString();
String message = txtMessage.getText().toString();

try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
}

catch (Exception e) {
Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}

@Override
protected void onDestroy() {
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
super.onDestroy();
}
}

关于android - 为什么我的广播接收器无法识别我的字符串 - 发送短信代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38161119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com