2012-11-24 116 views
0

我有一个名为MainActivity的活动。 在我开始另一个活动(ContentDetails)如何保持基本活动运行?

public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 

    Uri contactUri = ContentUris.withAppendedId(People.CONTENT_URI, id); 
    Intent intent = new Intent(this, ContactDetails.class); 
    intent.setData(contactUri); 
    startActivity(intent); 

} 

而在ContactDetails.java我已经写代码来拨打电话。 但是当我结束通话。它完成所有的应用程序,而不是重定向到基地活动

以下是ContactDetails.java

public class ContactDetails extends Activity{ 

    TextView nameField = null; 
    TextView phoneField = null; 
    TextView idField = null; 

    final Context context = this; 
    private Button button; 

    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.details); 

     idField = (TextView) findViewById(R.id.contact_id); 
     nameField = (TextView) findViewById(R.id.contact_name); 
     phoneField = (TextView) findViewById(R.id.contact_phone); 

     button = (Button) findViewById(R.id.call_button); 

     // add PhoneStateListener 
     PhoneCallListener phoneListener = new PhoneCallListener(); 

     TelephonyManager telephonyManager; 

     telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); 
     telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE); 

     // add button listener 
     button.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       Intent callIntent = new Intent(Intent.ACTION_CALL); 
       callIntent.setData(Uri.parse("tel:03577899456")); 
       startActivity(callIntent); 

      } 

     }); 


    } 

    @Override 
    protected void onStart() { 

     super.onStart(); 

     Cursor cursor = managedQuery(getIntent().getData(), null, null, null, null); 
     cursor.moveToFirst(); 

     //idField.setText(cursor.getString(cursor.getColumnIndex(People._ID))); 
     nameField.setText(cursor.getString(cursor.getColumnIndex(People.DISPLAY_NAME))); 
     phoneField.setText(cursor.getString(cursor.getColumnIndex(People.NAME))); 

    } 
    private class PhoneCallListener extends PhoneStateListener { 

     private boolean isPhoneCalling = false; 

     String LOG_TAG = "LOGGING 123"; 

     @Override 
     public void onCallStateChanged(int state, String incomingNumber) { 

      if (TelephonyManager.CALL_STATE_RINGING == state) { 
       // phone ringing 
       Log.i(LOG_TAG, "RINGING, number: " + incomingNumber); 
      } 

      if (TelephonyManager.CALL_STATE_OFFHOOK == state) { 
       // active 
       Log.i(LOG_TAG, "OFFHOOK"); 

       isPhoneCalling = true; 
      } 

      if (TelephonyManager.CALL_STATE_IDLE == state) { 
       // run when class initial and phone call ended, need detect flag 
       // from CALL_STATE_OFFHOOK 
       Log.i(LOG_TAG, "IDLE"); 

       if (isPhoneCalling) { 

        Log.i(LOG_TAG, "restart app"); 

        // restart app 
        try{ 
        // Intent intentAndroid = new Intent().setClass(this, ContactList.class); 
        Intent i = getBaseContext().getPackageManager() 
          .getLaunchIntentForPackage(getBaseContext().getPackageName()); 

        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(i); 
        }catch (Exception e) { 
         // TODO: handle exception 
        } 

        isPhoneCalling = false; 
       } 

      } 
     } 
    } 

} 

回答

3

实际上的代码,我认为你必须使用

startActivityForResult(callIntent, <RESULT_OK>); 

,而不是

startActivity(callIntent); 

覆盖onActivityResult()在您的联系方式详细信息活动。

试试这个,让我知道发生什么事..

而且从你的代码,我建议你不要使用国旗i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);作为其明确当前的活动状态的任务。