2016-03-17 32 views
1

我想更新ListViewBroadcastReceiveronReceiver 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

如何解決這個問題?

回答

0

啓動將使用Intents填充列表視圖的活動。

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

爲了避免同「Myactivity」的多個實例,該清單文件中使用相應的標誌。

如果你想你的廣播接收器和「Myactivity」之間傳輸的數據,或者使用intent.putExtra()或將數據存儲到sharedPreferences

+0

是的,我加了這段代碼:'sIntent =新意圖(context.getApplicationContext(), CallRecordService.class);' ' context.startService(sIntent);' –

+0

但我不知道如何初始化ListView'活動 –

+0

@JoeJoinshon,那麼你只需要閱讀有關使用listview或recyclerviews。 這是一個鏈接http://developer.android.com/training/material/lists-cards.html –

相關問題