如何从 BroadcastReceiver 更新 RecyclerView?

在AndroidManifest.xml我有一个BroadcastReceiver:


<receiver android:name=".SmsMonitor">

    <intent-filter android:priority="-100">

        <action android:name="android.provider.Telephony.SMS_RECEIVED" />

    </intent-filter>

</receiver>

在我的MainActivity中,我有RecyclerView一个收到的短信列表。


我的问题是 - 如果在应用程序运行时收到新的 SMS,我如何RecyclerView从我的BroadcastReceiver“SmsMonitor”更新?


UPD:我的意思是我需要两种状态的解决方案——在一个接收器中运行应用程序和应用程序未运行时的状态。因为我必须在电话工作时一直接收短信。如果应用程序正在运行,我想更新RecyclerView.


HUH函数
浏览 97回答 2
2回答

慕少森

我试图通过 来做到这一点Service,但我没有成功。但我找到了简单的解决方案:我还有BroadcastReceiver SmsMonitor,注册在AndroidManifest.xml在我创建我的实例的地方RecyclerAdapter:mainAdapter = new MainAdapter(this);在此之后我注册另一个BroadcastReceviver:BroadcastReceiver br = new BroadcastReceiver() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onReceive(Context context, Intent intent) {&nbsp; &nbsp; &nbsp; &nbsp; setInitialData();&nbsp; &nbsp; &nbsp; &nbsp; mainAdapter.notifyDataSetChanged();&nbsp; &nbsp; }};IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");this.registerReceiver(br, filter);在setInitialData()我重新创建所有数据列表RecyclerView。而现在我有两个独立工作BroadcastReceiver。第一个工作BroadcastReceiver来自AndroidManifest.xml,然后工作BroadcastReceiver在 MainActivity 中注册。

翻阅古今

您需要BrodcastReceiver在您的MainActivity或Fragment托管RecyclerView. 这样当收到新短信时,您可以调用您notifyDataSetChanged的适配器上的RecyclerView更新它。BroadcastReceiver在你Activity喜欢的下面声明一个。这可以是您班级的内部MainActivity班级。class SMSBroadcastReceiver extends BroadcastReceiver {&nbsp; &nbsp; private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";&nbsp; &nbsp; private static final String TAG = "SMSBroadcastReceiver";&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onReceive(Context context, Intent intent) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (intent.getAction() == SMS_RECEIVED) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bundle bundle = intent.getExtras();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (bundle != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Update your RecyclerView here&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // by calling notifyDataSetChanged on the adapter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;}}现在BroadcastReceiver在你的onCreate函数中创建一个实例MainActivity并注册接收器。您需要先在您Activity的下面请求短信许可。if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {&nbsp; &nbsp; ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.RECEIVE_SMS}, 0);} else {&nbsp; &nbsp; &nbsp; &nbsp; // register sms receiver&nbsp; &nbsp; &nbsp; &nbsp; IntentFilter filter = new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION);&nbsp; &nbsp; &nbsp; &nbsp; registerReceiver(smsReceiver, filter);}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {&nbsp; &nbsp; super.onRequestPermissionsResult(requestCode, permissions, grantResults);&nbsp; &nbsp; if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; // register sms receiver&nbsp; &nbsp; &nbsp; &nbsp; IntentFilter filter = new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION);&nbsp; &nbsp; &nbsp; &nbsp; registerReceiver(smsReceiver, filter);&nbsp; &nbsp; }}不要忘记在onDestroy您的MainActivity.希望有帮助!更新根据此答案下方的评论,您有两种情况。案例 1. 应用程序正在运行。案例 2. 应用程序未运行。对于案例1:BroadcastReceiver像我上面显示的本地,足以RecyclerView在新短信到达时立即更新。当您处理BroadcastReceiverfrom 时,MainActivity这应该不是问题,因为您参考了RecyclerView.现在来看案例 2:如果您能够检测到任何新的 SMS 到达,您可以将收到的新 SMS 保存在本地数据库中。您需要为您的应用程序提供一个 SQLite 数据库,该数据库将存储所有 SMS,并且RecyclerView在您的应用程序中应该从存储 SMS 的数据库表中填充数据。我希望你明白 - 只需将 SMS 存储在与应用程序关联的本地数据库中,当你运行应用程序并启动时MainActivity,从本地数据库中读取 SMS 并将它们显示在你的RecyclerView.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java