在 Handler 不工作的情况下每秒更新一次 ListView
Update ListView every second with Handler not working
在我的应用程序接收新短信的应用程序中,我希望列表视图使用列表视图中显示的新消息立即更新它。我尝试使用广播接收器代码的示例代码,但它没有用,所以我现在尝试使用计时器每秒更新一次列表视图并显示收到的最新消息,但这也不起作用。
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;
最初尝试在收到新短信时使用 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();
}
将列表视图更新代码放在列表视图所属的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
}
}
还要添加到答案中,不要忘记在 AndroidManifest.xml 文件
下方添加此内容
</activity>
<receiver android:name=".SmsBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
以上也为我解决了这个问题。谢谢你们的帮助。
在我的应用程序接收新短信的应用程序中,我希望列表视图使用列表视图中显示的新消息立即更新它。我尝试使用广播接收器代码的示例代码,但它没有用,所以我现在尝试使用计时器每秒更新一次列表视图并显示收到的最新消息,但这也不起作用。
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;
最初尝试在收到新短信时使用 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();
}
将列表视图更新代码放在列表视图所属的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
}
}
还要添加到答案中,不要忘记在 AndroidManifest.xml 文件
下方添加此内容 </activity>
<receiver android:name=".SmsBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
以上也为我解决了这个问题。谢谢你们的帮助。