-1

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

 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();
    }
4

2 回答 2

1

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

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

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

于 2019-04-08T06:27:56.243 回答
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        
   }

}
于 2019-04-08T05:12:59.440 回答