意图功能无法正常工作

时间:2014-05-14 06:34:34

标签: android android-intent

我正面临一个问题,就是我通过putExtra从电话活动发送一个行号,并在拨号活动中收到该值。第一次getIntent函数收到正确的值,但在第二次他无法收到正确的值。第一个谷是第二次展示。

电话活动

    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();
    }


 }

2 个答案:

答案 0 :(得分:0)

Phone活动使用DialPad启动Intent.FLAG_ACTIVITY_REORDER_TO_FRONT活动。这意味着如果任务堆栈中已存在DialPad的活动(未完成)实例,则此活动实例将简单地移动到前端(活动堆栈的顶部)。在这种情况下,onCreate()上的DialPad 将不会被调用,因为它不会重新创建活动。而是会调用onNewIntent()。您尚未在DialPad中覆盖此方法,因此您将看不到此方法调用。

由于您要从LineNo中的Intent extras中提取PhoneNoonCreate(),因此您将始终只使用从第一个Intent获得的数据。这正是你所看到的问题。

要解决此问题,请覆盖onNewIntent()并从LineNo中收到的PhoneNo参数中传递的附加内容中提取IntentonNewIntent()

答案 1 :(得分:-1)

尝试创建一个Bundle对象然后通过intent.putExtras(bundle)传递它,这是通过intent传递额外内容的正确方法(我理解nFreeLineNo是一个int而sPhoneNo是一个String):

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);

另外,在DialPad 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发布错误堆栈跟踪