我有一个问题,没有出现烤面包。 但是,显示了第一和第三敬酒,第二敬酒使用了完全相同的语法。每次上下文都应该相同。
我的应用程序没有崩溃,我已经尝试runOnUiThread()
无济于事。
我已经寻找答案已有一段时间了,但似乎找不到问题背后的原因。
预先感谢您的帮助
这不是整个课程,而是在这里发生
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);
}
}
}
答案 0 :(得分:2)
您的代码正在UI线程上运行,并且Thread.sleep()
将其阻止。 UI线程无法处理其他任何事情,例如在屏幕上显示吐司。
请勿在UI线程上使用Thread.sleep()
。
您需要其他一些机制来处理异步操作,例如等待完成。回调方法是一种常用的方法。
答案 1 :(得分:0)
请使用活动上下文“ this
”而不是getApplicationContext()
。应该可以。
注意:理想情况下,根据文档,它应与活动或应用程序上下文一起使用。但是从我的经验来看,应用程序上下文并非一直有效。
也不要在活动中使用Thread.sleep()
方法。如果您正在寻找等待功能,请使用带有postDelayed
的用户Handler
方法
例如:
final Handler handler = new Handler();
handler.postDelayed (() -> {
//your code here
}, TIME_DELAY);