1
我想更新ListView
從BroadcastReceiver
在onReceiver event
。如何使用BroadcastReceiver更新ListView?
如何初始化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
類。
而且我不能使用getActivity()
類或findViewById(a, b)
擴展時BroadcastReceiver
。
如何解決這個問題?
是的,我加了這段代碼:'sIntent =新意圖(context.getApplicationContext(), CallRecordService.class);' ' context.startService(sIntent);' –
但我不知道如何初始化ListView'活動 –
@JoeJoinshon,那麼你只需要閱讀有關使用listview或recyclerviews。 這是一個鏈接http://developer.android.com/training/material/lists-cards.html –