android toast 未显示(尽管另一个显示)
android toast not showing (though another does)
我遇到了一个问题,就是没有显示吐司。
但是显示了第一个和第三个祝酒词,而第二个祝酒词使用完全相同的语法。上下文每次都应该相同。
我的应用程序没有崩溃,我已经尝试 runOnUiThread()
无济于事。
我一直在寻找答案已经有一段时间了,但似乎找不到问题背后的原因。
在此先感谢您的帮助
这不是全部class,但这就是它发生的地方
public class SendActivity extends AppCompatActivity {
private static final int PICK_FILE = 0;
private static final int REQUEST_PERMISSION_SMS_SEND = 1;
private static final int REQUEST_PERMISSION_SMS_READ = 2;
public static final int MESSAGE_CAPACITY = 114; //amount of bytes that can be transmitted per message //TODO check value
public static final long MAX_FILE_SIZE = 400_000; //TODO replace with exact value (bytes)
private Long fileSize;
private String fileName;
private android.net.Uri uri;
private boolean inputOK;
private boolean receivedAuthorization;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
}
/**
* Method called when the confirmation button is pressed. It will go to the transmitActivity
* if it passes the verification
*
* @param view the view that called the method
*/
public void transmitActivity(View view) {
TextView tv = findViewById(R.id.numberInputInner);
String number = tv.getText().toString();
if (!inputOK || number.isEmpty() || !PhoneNumberUtils.isWellFormedSmsAddress(number)) { //only check if number is present and numerical
// 1 SHOWN
Toast.makeText(this, "Please enter a phone number and choose a correct file", Toast.LENGTH_LONG).show();
return;
}
//check if allowed to send sms, else request
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, REQUEST_PERMISSION_SMS_SEND);
return;
}
//check if allowed to read sms, else request
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, REQUEST_PERMISSION_SMS_READ);
return;
}
try {
receivedConfirmation = false;
sendRequestMessage(); //sends an sms
listenForResponse(); //creates a thread listening for response
// 2 NOT SHOWN
Toast.makeText(getApplicationContext(), "Waiting 15 sec for authorization from other party", Toast.LENGTH_LONG).show();
Thread.sleep(15_000);
} catch (InterruptedException e) {
//TODO
}
if (!receivedConfirmation) {
// 3 SHOWN
Toast.makeText(this, "Didn't receive authorization to transfer the file. Try again", Toast.LENGTH_LONG).show();
return;
} else {
Intent transmitActivity = new Intent(this, TransmitActivity.class);
transmitActivity.putExtra("fileName", this.fileName);
transmitActivity.putExtra("fileSize", this.fileSize);
transmitActivity.putExtra("uri", this.uri);
transmitActivity.putExtra("phoneNumber", tv.getText().toString());
startActivity(transmitActivity);
}
}
}
您的代码在 UI 线程上 运行 并且 Thread.sleep()
阻止了它。 UI 线程不会处理其他任何事情,例如在屏幕上显示祝酒词。
切勿在 UI 线程上使用 Thread.sleep()
。
您需要一些其他机制来处理异步操作,例如等待某事完成。回调方法是一种常用的方法。
请使用 activity 上下文“this
”而不是 getApplicationContext()
。应该可以。
注意:理想情况下,根据文档,它应该与 activity 或应用程序上下文一起使用。但根据我的经验,应用程序上下文并不是一直有效。
也不要在activity中使用Thread.sleep()
方法。如果您正在寻找等待功能,请使用 postDelayed
方法和 Handler
例如:
final Handler handler = new Handler();
handler.postDelayed (() -> {
//your code here
}, TIME_DELAY);
我遇到了一个问题,就是没有显示吐司。 但是显示了第一个和第三个祝酒词,而第二个祝酒词使用完全相同的语法。上下文每次都应该相同。
我的应用程序没有崩溃,我已经尝试 runOnUiThread()
无济于事。
我一直在寻找答案已经有一段时间了,但似乎找不到问题背后的原因。
在此先感谢您的帮助
这不是全部class,但这就是它发生的地方
public class SendActivity extends AppCompatActivity {
private static final int PICK_FILE = 0;
private static final int REQUEST_PERMISSION_SMS_SEND = 1;
private static final int REQUEST_PERMISSION_SMS_READ = 2;
public static final int MESSAGE_CAPACITY = 114; //amount of bytes that can be transmitted per message //TODO check value
public static final long MAX_FILE_SIZE = 400_000; //TODO replace with exact value (bytes)
private Long fileSize;
private String fileName;
private android.net.Uri uri;
private boolean inputOK;
private boolean receivedAuthorization;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
}
/**
* Method called when the confirmation button is pressed. It will go to the transmitActivity
* if it passes the verification
*
* @param view the view that called the method
*/
public void transmitActivity(View view) {
TextView tv = findViewById(R.id.numberInputInner);
String number = tv.getText().toString();
if (!inputOK || number.isEmpty() || !PhoneNumberUtils.isWellFormedSmsAddress(number)) { //only check if number is present and numerical
// 1 SHOWN
Toast.makeText(this, "Please enter a phone number and choose a correct file", Toast.LENGTH_LONG).show();
return;
}
//check if allowed to send sms, else request
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, REQUEST_PERMISSION_SMS_SEND);
return;
}
//check if allowed to read sms, else request
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, REQUEST_PERMISSION_SMS_READ);
return;
}
try {
receivedConfirmation = false;
sendRequestMessage(); //sends an sms
listenForResponse(); //creates a thread listening for response
// 2 NOT SHOWN
Toast.makeText(getApplicationContext(), "Waiting 15 sec for authorization from other party", Toast.LENGTH_LONG).show();
Thread.sleep(15_000);
} catch (InterruptedException e) {
//TODO
}
if (!receivedConfirmation) {
// 3 SHOWN
Toast.makeText(this, "Didn't receive authorization to transfer the file. Try again", Toast.LENGTH_LONG).show();
return;
} else {
Intent transmitActivity = new Intent(this, TransmitActivity.class);
transmitActivity.putExtra("fileName", this.fileName);
transmitActivity.putExtra("fileSize", this.fileSize);
transmitActivity.putExtra("uri", this.uri);
transmitActivity.putExtra("phoneNumber", tv.getText().toString());
startActivity(transmitActivity);
}
}
}
您的代码在 UI 线程上 运行 并且 Thread.sleep()
阻止了它。 UI 线程不会处理其他任何事情,例如在屏幕上显示祝酒词。
切勿在 UI 线程上使用 Thread.sleep()
。
您需要一些其他机制来处理异步操作,例如等待某事完成。回调方法是一种常用的方法。
请使用 activity 上下文“this
”而不是 getApplicationContext()
。应该可以。
注意:理想情况下,根据文档,它应该与 activity 或应用程序上下文一起使用。但根据我的经验,应用程序上下文并不是一直有效。
也不要在activity中使用Thread.sleep()
方法。如果您正在寻找等待功能,请使用 postDelayed
方法和 Handler
例如:
final Handler handler = new Handler();
handler.postDelayed (() -> {
//your code here
}, TIME_DELAY);