在处理程序不起作用的情况下每秒更新ListView

时间:2019-04-08 04:52:26

标签: android android-fragments android-listfragment android-sms

在我的应用程序接收到新短信的应用程序中,我希望列表视图立即使用列表视图中显示的新消息对其进行更新。我尝试使用广播接收器代码的示例代码,但是它没有用,所以我现在尝试使用计时器每秒更新一次列表视图,并显示收到的最新消息,但这也不起作用。

 final Handler handler = new Handler();
                handler.postDelayed(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        inboxArrayAdapter.notifyDataSetChanged();

                        ((BaseAdapter)inboxmessages.getAdapter()).notifyDataSetChanged();

                        handler.postDelayed(this,  1000 );

                    }
                }, 1000);

        return view;

收到新的SMS消息时,最初尝试使用BroadcastReceiver更新列表视图。

SmSBroadcastReceiver.java

public class SmsBroadcastReceiver extends BroadcastReceiver
{
    public static final String SMS_BUNDLE = "pdus";

    // https://javapapers.com/android/android-receive-sms-tutorial/
    public void onReceive(Context context, Intent intent)
    {
        Bundle intentExtras = intent.getExtras();
        if (intentExtras != null)
        {
            Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
            String smsMessageStr = "";

            for (int i = 0; i < sms.length; i++)
            {
                // Get sms message
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

                // Get content and number
                String smsBody = smsMessage.getMessageBody().toString();
                String address = smsMessage.getOriginatingAddress();

                // Create display message
                smsMessageStr += "SMS From: " + address + "\n";
                smsMessageStr += smsBody + "\n";

                // Add it to the list
                CommandsFragment inst = CommandsFragment.instance();
                inst.updateList(smsMessageStr);
            }
            CommandsFragment inst = CommandsFragment.instance();
            inst.updateList(smsMessageStr);
        }
    }
}

CommandsFragment.java

 public static CommandsFragment instance()
        {
            return inst;
        }


        @Override
        public void onStart() {
            super.onStart();
            inst = this;
        }


        public static CommandsFragment newInstance()
        {
            CommandsFragment fragment = new CommandsFragment();
            return fragment;
        }

 public void updateList(final String smsMessage)
    {
        inboxArrayAdapter.insert(smsMessage, 0);
        inboxArrayAdapter.notifyDataSetChanged();
    }

2 个答案:

答案 0 :(得分:1)

将listview更新代码放入listview所属的Activity中的方法内,然后从广播接收器的onReceive调用该方法。肯定会的。还要检查代码是否正确更新了列表视图。

public class SmsBroadcastReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context paramContext, Intent intent) { // change the parameters depending upon your requirements

    // do something here with the sms received if necessary

    // then call the listview update method here        
   }

}

答案 1 :(得分:1)

还要添加到答案中,请不要忘记在AndroidManifest.xml文件中添加以下内容

npm install -g @angular/cli@1.4.9

以上内容也为我解决了问题。感谢您的帮助。