2012-11-24 82 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);作爲其明確當前的活動狀態的任務。