0

我想在按下返回按钮后启动我的应用程序(例如更新我的 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"); 

    }
};
4

1 回答 1

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"));
            }

        }
    };
于 2012-05-14T09:03:38.973 回答