如何使用BroadcastReceiver更新ListView?

时间:2016-03-17 20:10:37

标签: android listview

我希望ListView中的BroadcastReceiver更新onReceiver event

如何在此课程中初始化ListView,然后更新为ListView

public class CallBroadcast extends BroadcastReceiver {

    private static int pState = TelephonyManager.CALL_STATE_IDLE;

    @Override
    public void onReceive(final Context context, final Intent intent) {
        TelephonyManager telManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        telManager.listen(new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                callDAO = new CallDAO(context);
                if (configPreferenceManager.getAutoRecord()) {
                    if (state != pState) {
                        if (state == TelephonyManager.CALL_STATE_OFFHOOK && callInfoPreferenceManager.getCallState()) {
                            uri = Uri.withAppendedPath(
                                ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                                Uri.encode(callInfoPreferenceManager.getPhoneNumber()));
                            projection = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                                    , ContactsContract.Contacts.PHOTO_ID
                            };


                            // START SERVICES TO RECORD CALL. AFTER IT, I WANT UPDATE MY LIST
                            sIntent = new Intent(context.getApplicationContext(),
                                    CallRecordService.class);
                            context.startService(sIntent);
                        } else if (state == TelephonyManager.CALL_STATE_RINGING && callInfoPreferenceManager.getCallState()) {
                            callInfoPreferenceManager.setPhoneNumber(incomingNumber);
                            callInfoPreferenceManager.setSending(String.valueOf(false));

                        } else if (state == TelephonyManager.CALL_STATE_IDLE && callInfoPreferenceManager.getCallState() == CALLING) {
                                callDAO.insert(callInfoPreferenceManager.getName(),
                                    callInfoPreferenceManager.getPhoneNumber(),
                                    callInfoPreferenceManager.getStartDate()
                                            + configPreferenceManager.getPathFormat());
                            callDAO.close();

                            // Record call start service
                            sIntent = new Intent(context.getApplicationContext(),
                                    CallRecordService.class);
                            context.stopService(sIntent);
                            callInfoPreferenceManager.setCallState(IDLE);

                        }
                        pState = state;
                    }
                }
            }
        }, PhoneStateListener.LISTEN_CALL_STATE);
    }//onReceive

}

我发布的代码是BroadcastReceiver class。

我在扩展getActivity()时无法使用findViewById(a, b)课程或BroadcastReceiver

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

启动将使用Intents填充列表视图的活动。

Intent intent = new Intent(context,Myactivity.class)
startactivity(intent);

要避免多个相同“Myactivity”的实例,请在清单文件中使用适当的标记。

如果您想在broadcastReceiver和“Myactivity”之间传输数据,请使用intent.putExtra()或将数据存储到sharedPreferences