2014-05-14 256 views
-4

即時通訊面臨的一個問題是,即時通過putExtra從電話活動發送線路號碼,並收到撥號活動的值。 getIntent函數第一次收到正確的值,但在第二次時,他無法收到正確的值。第1個谷值第2次出現。意圖功能無法正常工作

手機活動

public class Phone extends Activity 
{ 


    List<Map<String, String>> data; 

    Map<String, String> Line1; 
    Map<String, String> Line2; 
    Map<String, String> Line3; 
    Map<String, String> Line4; 
    Map<String, String> Line5; 

    ListView Lines = null; 

    public void onCreate(final Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     m_objPhone = this; 

     String sPhoneAccountStatus = VaxStatus.m_objVaxStatus.GetPhoneAccountStatus(); 
     m_objPhoneAccountStatus.setText(sPhoneAccountStatus); 

     data = new ArrayList<Map<String, String>>(); 

     Line1 = new HashMap<String, String>(); 

     Line1.put("title", "Line | 1"); 
     Line1.put("status", ""); 

     data.add(Line1); 


     Line2 = new HashMap<String, String>(); 

     Line2.put("title", "Line | 2"); 
     Line2.put("status", ""); 
     data.add(Line2); 

     Line3 = new HashMap<String, String>(); 

     Line3.put("title", "Line | 3"); 
     Line3.put("status", ""); 
     data.add(Line3); 

     Line4 = new HashMap<String, String>(); 

     Line4.put("title", "Line | 4"); 
     Line4.put("status", ""); 
     data.add(Line4); 

     Line5 = new HashMap<String, String>(); 

     Line5.put("title", "Line | 5"); 
     Line5.put("status", ""); 
     data.add(Line5); 

     SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, new String[] {"title", "status"}, new int[] {android.R.id.text1, android.R.id.text2}); 

     Lines.setAdapter(adapter); 

     for (int nLineNo = 0; nLineNo < VaxSIPUserAgent.TOTAL_LINE_COUNT; nLineNo++) 
     { 
      String sLineStatus = VaxStatus.m_objVaxStatus.GetLineStatus(nLineNo); 
      SetStatus(nLineNo, sLineStatus); 
     } 


     Lines.setOnItemClickListener(new OnItemClickListener() 
     { 

      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
      { 
       switch (arg2) 
       { 
        case 0: 
         ShowDialPad(arg2, null); 

         break; 
        case 1: 
         ShowDialPad(arg2, null); 

         break; 
        case 2: 
         ShowDialPad(arg2, null); 

         break; 
        case 3: 
         ShowDialPad(arg2, null); 

         break; 
        case 4: 
         ShowDialPad(arg2, null); 

         break; 

        default: 
         break; 
       } 

      } 

     }); 

    } 

    @Override 
    protected void onResume() 
    { 
     for (int nLineNo = 0; nLineNo < VaxSIPUserAgent.TOTAL_LINE_COUNT; nLineNo++) 
     { 
      String sLineStatus = VaxStatus.m_objVaxStatus.GetLineStatus(nLineNo); 
      SetStatus(nLineNo, sLineStatus); 
     } 

     super.onResume(); 
    } 

    @Override 
    protected void onStart() 
    { 
     VaxPhone.m_objVaxVoIP.IsMuteMic(); 
     VaxPhone.m_objVaxVoIP.IsMuteSpk(); 

     IncommingCallSlider.m_objCallSlider.SetActiveActivity(this, R.id.PhoneRelativeLayout); 
     IncommingCallRing.m_objIncommingCallRing.SetActiveActivity(this); 

     DialCallTone.m_objDialCallTone.SetActiveActivity(this); 

     super.onStart(); 
    } 


    @Override 
    protected void onStop() 
    { 
     VaxPhone.m_objVaxVoIP.SetSpeakerState(m_objcheckSpkPhone.isChecked()); 

     super.onStop(); 
    } 
    @Override 
    protected void onDestroy() 
    { 
     VaxStatus.m_objVaxStatus.SetPhoneActivity(null); 

     super.onDestroy(); 
    } 

    public void OnEndCall(int nLineNo) 
    { 
     SetStatus(nLineNo, "Ready To Use!"); 

    } 

    public static void OnContactPhoneNo(String sPhoneNo) 
    { 
     int nFreeLineNo = VaxPhone.m_objVaxVoIP.GetFreeLine(); 
     m_objPhone.ShowDialPad(nFreeLineNo, sPhoneNo); 
    } 


    public void ShowDialPad(int nFreeLineNo, String sPhoneNo) 
    { 
     Intent intent = new Intent(Phone.this , DialPad.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
     intent.putExtra("LineNo", nFreeLineNo); 
     intent.putExtra("PhoneNo", sPhoneNo); 
     startActivity(intent); 

    } 

    public void OnStatusPhone(int nLineNo, String sStatusMsg) 
    { 
     for (int LineNo = 0; LineNo < VaxSIPUserAgent.TOTAL_LINE_COUNT; LineNo++) 
     { 
      String sLineStatus = VaxStatus.m_objVaxStatus.GetLineStatus(LineNo); 
      SetStatus(LineNo, sLineStatus); 
     } 

     if(m_objDialPad != null) 
      m_objDialPad.OnStatusPhone(sStatusMsg); 
    } 

    public void OnStatusAccount(String sStatusMsg) 
    { 
     m_objPhoneAccountStatus.setText(sStatusMsg); 

     if(m_objDialPad != null) 
      m_objDialPad.OnStatusAccount(sStatusMsg); 
    } 




    public void showSelectedNumber(String number) 
    { 
     Phone.OnContactPhoneNo(number); 
    } 

    void SetStatus(int nLineNo, String sStatusMsg) 
    { 
     if(nLineNo == 0) 
     { 
      Line1 = new HashMap<String, String>(); 
      Line1.put("title", "Line | 1"); 
      Line1.put("status", sStatusMsg); 
      data.set(0, Line1); 

      SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, new String[] {"title", "status"}, new int[] {android.R.id.text1, android.R.id.text2}); 

      Lines.setAdapter(adapter); 
     } 

     if(nLineNo == 1) 
     { 
      Line2 = new HashMap<String, String>(); 
      Line2.put("title", "Line | 2"); 
      Line2.put("status", sStatusMsg); 
      data.set(1, Line2); 
      SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, new String[] {"title", "status"}, new int[] {android.R.id.text1, android.R.id.text2}); 

      Lines.setAdapter(adapter); 
     } 

     if(nLineNo == 2) 
     { 
      Line3 = new HashMap<String, String>(); 
      Line3.put("title", "Line | 3"); 
      Line3.put("status", sStatusMsg); 
      data.set(2, Line3); 
      SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, new String[] {"title", "status"}, new int[] {android.R.id.text1, android.R.id.text2}); 

      Lines.setAdapter(adapter); 
     } 
     if(nLineNo == 3) 
     { 
      Line4 = new HashMap<String, String>(); 
      Line4.put("title", "Line | 4"); 
      Line4.put("status", sStatusMsg); 
      data.set(3, Line4); 
      SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, new String[] {"title", "status"}, new int[] {android.R.id.text1, android.R.id.text2}); 

      Lines.setAdapter(adapter); 
     } 
     if(nLineNo == 4) 
     { 
      Line5 = new HashMap<String, String>(); 
      Line5.put("title", "Line | 5"); 
      Line5.put("status", sStatusMsg); 
      data.set(4, Line5); 
      SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, new String[] {"title", "status"}, new int[] {android.R.id.text1, android.R.id.text2}); 

      Lines.setAdapter(adapter); 
     } 

    } 
} 

撥號活動

public class DialPad extends Activity 
{ 
    private TextView m_objStatus; 
    private Button m_objBtnDial; 




    int m_nLineNo = -1; 
    boolean m_objContactFlag; 

    public static Phone m_objPhone; 
    private static String[] m_objPhoneNum = new String[VaxSIPUserAgent.TOTAL_LINE_COUNT]; 

    public void onCreate(Bundle savedInstanceState) 
    { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     Phone.m_objDialPad = this; 

     Intent intent = getIntent(); 

     int nIntentExtraLineNo = intent.getIntExtra("LineNo", 1); 
     m_nLineNo = nIntentExtraLineNo; 



     final int [] aLineArray = new int[VaxSIPUserAgent.TOTAL_LINE_COUNT - 1]; 

     int nIndex = 0; 

     for (int nLineNo = 0; nLineNo < VaxSIPUserAgent.TOTAL_LINE_COUNT; nLineNo++) 
     { 
      if(m_nLineNo == nLineNo) 
       continue; 

      aLineArray[nIndex] = nLineNo + 1; 
      nIndex++; 

     } 

     String sLineStatus = VaxStatus.m_objVaxStatus.GetLineStatus(m_nLineNo); 
     OnStatusPhone(sLineStatus); 


      String sPhoneNo = intent.getStringExtra("PhoneNo"); 

     if(sPhoneNo != null) 
     { 
      m_objPhoneNum[nIntentExtraLineNo] = sPhoneNo; 
     } 

     m_objEditDialscreen.setText(m_objPhoneNum[nIntentExtraLineNo]); 
    } 




    @Override 
    protected void onStart() 
    { 



     UpdateDialBtn(); 


     if(m_objContactFlag == false) 
      m_objEditDialscreen.setText(m_objPhoneNum[m_nLineNo]); 

     IncommingCallSlider.m_objCallSlider.SetActiveActivity(this, R.id.dialpad); 
     IncommingCallRing.m_objIncommingCallRing.SetActiveActivity(this); 

     DialCallTone.m_objDialCallTone.SetActiveActivity(this); 

     super.onStart(); 
    } 



    @Override 
    protected void onStop() 
    { 
     m_objPhoneNum[m_nLineNo] = m_objEditDialscreen.getText().toString(); 
     super.onStop(); 
    } 


} 
+0

這是'Phone.java'類的所有代碼嗎? –

+0

檢查你是否在第二次傳遞相同的'sPhoneNo和nFreeLineNo' – SathishKumar

+0

你是否在AndroidManifest的DialPad Activity中有任何標誌,如SINGLE_TOP或smth else。如果是這樣的創建可能不會被調用。在這種情況下,您必須重寫activity中的onNewIntent()方法。 –

回答

-1

嘗試創建一個Bundle對象,然後通過intent.putExtras(束)傳遞,這是通過一個意圖通過額外的正確方法(我明白nFreeLineNo是一個int和sPhoneNo一個字符串):

Intent intent = new Intent(Phone.this , DialPad.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
Bundle b = new Bundle(); 
b.putInt("LineNo", nFreeLineNo); 
b.putString("PhoneNo", sPhoneNo); 
intent.putExtras(b); 
startActivity(intent); 

另外,在DialPa d getExtras這樣的:

Intent intent = getIntent(); 
Bundle b = intent.getExtras(); 
//Make sure you passed a bundle 
if (b != null){ 
    m_nLineNo = b.getInt("LineNo"); 
    m_sPhoneNo = b.getString("PhoneNo"); 
}else{ 
    //didnt pass bundle, Do what you want 
} 

希望我幫你,我也有這個問題,解決了這個方式

如果不幫你,請從logcat的POST錯誤堆棧跟蹤

+0

不是它不工作 – user3136235

+0

請發佈stacktrace然後。否則,我忍不住 – BamsBamx

+0

這是一個無用的答案。其他的東西已經是'Bundle'了。你可以簡單地使用'putExtra()'(這個OP已經在做)來添加原語和'String'。把這些額外的東西放到一個額外的Bundle中,然後調用putExtras()將這個Bundle中的數據複製到額外的Bundle中,只是簡單的開銷,並不能完成任何事情。最後,它和OP已經做的完全一樣(這就是爲什麼這個答案不能解決他的問題)。 –

0

Phone活動啓動DialPad活動使用Intent.FLAG_ACTIVITY_REORDER_TO_FRONT。這意味着如果任務堆棧中已有活動(未完成)DialPad實例,則該活動實例將簡單地移到前面(活動堆棧的頂部)。在這種情況下,onCreate()將不會在DialPad上被調用,因爲它不會重新創建活動。相反onNewIntent()將被調用。您尚未覆蓋DialPad中的此方法,因此您不會看到此方法調用。

既然你是在onCreate()提取的意圖演員LineNoPhoneNo,你永遠只使用您從第一Intent獲取數據。這正是你所看到的問題。

爲了解決這個問題,覆蓋onNewIntent()並提取從在您在onNewIntent()收到Intent參數傳遞的額外LineNoPhoneNo