从Sms BroadcastReceiver更新Listview

时间:2013-08-11 04:09:54

标签: android broadcastreceiver

我在writeMessage活动中有一个列表视图。当用户写入消息时,它将显示它写的内容但是当收到消息时它不会。我必须返回并再次启动活动。我试图将广播注册到我的活动但我不能

我定义了我的活动的顶部:

public class CMesWriteMessage extends Activity {
     private BroadcastReceiver the_receiver = new IncomingSms(){

         @Override
         public void onReceive(Context c, Intent i) {
             addItems();

         }
     };
     private IntentFilter filter = new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED);

...
...

}

并将其注册为:

protected void onResume() {

    // Register reciever if activity is in front
    this.registerReceiver(the_receiver, filter);
    super.onResume();
}

protected void onPause() {

     // Unregister reciever if activity is not in front
     this.unregisterReceiver(the_receiver);
     super.onPause();
}

我哪里错了?

2 个答案:

答案 0 :(得分:0)

我不知道这是否是整个问题,但super.onPause()/ super.onResume()应该是他们各自覆盖的第一个调用:

protected void onResume() {
    super.onResume();
    this.registerReceiver(the_receiver, filter);
}

protected void onPause() {
    super.onPause()
    this.unregisterReceiver(the_receiver);
}

答案 1 :(得分:0)

好的,我改变了我的行动,现在一切都很好。这是正确的代码:

private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
private IntentFilter filter = new IntentFilter(ACTION);

编辑:

我在我的广播课程中进行主要操作。在此活动中,我只更新listview。所以为了达到这个目的,我编辑我的代码:

private BroadcastReceiver the_receiver = new IncomingSms(){

         @Override
         public void onReceive(Context c, Intent i) {
             super.onReceive(c, i);
             //My activity special operations.

         }
};  

注意:IncomingSms是我的主要广播接收器类。