Handler无法正常工作,每秒更新ListView

问题描述 投票:-1回答:2

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

 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更新列表视图的原始尝试。

SMS broadcast receiver.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);
        }
    }
}

commands fragment.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();
    }
android android-fragments android-listfragment android-sms
2个回答
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
投票

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

  </activity>
        <receiver android:name=".SmsBroadcastReceiver">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>

以上也解决了我的解决方案。谢谢你的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.