我想在按下后退按钮后启动我的应用程序(更新我的TextView)。但它没有用。请帮帮我。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");
registerReceiver(intentReceiver, intentFilter);
}
protected void onPause() {
registerReceiver(intentReceiver, intentFilter);
super.onPause();
}
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
sendSMS("123","message");
}
};
答案 0 :(得分:0)
我已经更新了你的代码尝试这个,如果有任何问题,那么问我。我会尽力帮助你......
@Override
public void onBackPressed() {
// FOR UPDATING YOUR TEXT VIEW.
Intent broadcast_intent = new Intent("SMS_RECEIVED_ACTION");
broadcast_intent.putExtra("number", textview_value_to_set);
sendBroadcast(broadcast_intent);
// // FOR STARTING YOUR NEW ACTIVITY.
Intent intent = new Intent(getApplicationContext(), class_name.class);
startActivity(intent);
super.onBackPressed();
}
将此用作广播接收器。别忘了注册接收者。
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("SMS_RECEIVED_ACTION")) {
TextView text = (TextView)findViewById(R.id.text);
text.setText(intent.getExtras().getString("number"));
}
}
};