我试图在Handler中使用消息集。当我运行代码时,我的文本视图中没有显示任何内容。这很奇怪,因为Handler正在使用主UI looper messagequeue,所以我认为它包含了我的消息......下面是我的代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtBalance = (TextView) findViewById(R.id.txtBalance);
checkBalance();
}
private void checkBalance() {
myBalanceHandler = new Handler(Looper.getMainLooper()) {
public void handleMessage (Message m) {
String balance = (String) m.obj;
txtBalance.setText("Balance - " + balance);
}
};
new Thread() {
public void run() {
Intent intent = new Intent(Intent.ACTION_CALL,
Uri.parse("tel:" + Uri.encode("#745*8*6321#")));
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CALL_PHONE},
MY_PERMISSION_CALL_PHONE);
}
startActivity(intent);
try {
Thread.sleep(10000); //WAIT until USSD response is recieved
} catch (InterruptedException e) {
e.printStackTrace();
}
SMSGlobalClass smsGlobalClass = (SMSGlobalClass) getApplicationContext(); //Set in a broadcast listener
Message m = myBalanceHandler.obtainMessage();
m.obj = smsGlobalClass.getSms();
myBalanceHandler.sendMessage(m);
}
}.start();
}
}