2013-07-16 47 views
3

我從Activity撥打電話,當通話結束時,我想回到應用程序。我嘗試了所有可用的解決方案。其中一人曾經工作幾分鐘,但現在不工作。開始活動不會將應用程序放在前臺

我試過使用recreate()方法成功調用onCreate方法Activity,但應用程序不在前臺。我嘗試使用各種標誌,如FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_CLEAR_TASK,FLAG_ACTIVITY_NO_HISTORY。但不起作用。

代碼來調用應用程序返回到應用程序:

private class PhoneCallListener extends PhoneStateListener { 

    private boolean isPhoneCalling = false; 

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

     // If call ringing 
     if (state == TelephonyManager.CALL_STATE_RINGING) { 

     } 
     // Else if call active 
     else if (state == TelephonyManager.CALL_STATE_OFFHOOK) { 

      isPhoneCalling = true; 
     } 
     // Else if call idle 
     else if (state == TelephonyManager.CALL_STATE_IDLE) { 

      if (isPhoneCalling) { 

       isPhoneCalling = false; 

       MyActivity.this.recreate(); 
      } 
     } 
    } 
} 
+0

重複的http://stackoverflow.com/questions/17654608/coming-back-to-an-activity-after-making-a-phone-call – Jules

+0

@Jules這不重複。這個問題是如何在通話後開始同樣的活動。我可以用'recreate()'方法來做到這一點。現在,活動重新開始,但問題是如何把它放在前臺。 – Geek

+0

我有同樣的問題。你有什麼新想法嗎? – keybee

回答

0

解決的辦法是推出與應用包含在捆綁,指出應用啓動從任何其他應用程序回來的一部分extra

當通話結束時,我用下面的代碼啓動應用。

// Launch app 
Intent i = new Intent(ActivityThatMadeCall.this, 
LauncherActivity.class); 
i.putExtra("EXTRA_RETURNED_FROM_CALL_APP", true); 
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(i); 


然後在我的發射活動我檢查了extra,如果它存在,然後啓動放置呼叫activty。 :

@Override 
protected void onCreate(Bundle savedInstanceState) { 

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

    if(getIntent().getExtras().getString("EXTRA_RETURNED_FROM_CALL_APP") != null) { 
     startActivity(new Intent(this, ActivityThatMadeCall.class)); 
    } 
} 
0

我認爲你必須使用一個廣播接收器,並在uphook啓動活動。你也需要聲明讀取手機狀態的權限。

+0

我使用PhoneStateListener,它和通知一樣,所以我認爲不需要使用廣播接收器。我已添加權限。 – Geek

0
  1. 嘗試修改你的代碼,使重要的東西是不是在onCreate()方法,但在的onResume()方法,因爲當你從電話回來,在onCreate()方法不會被觸發但是onResume()方法。請參閱http://developer.android.com/reference/android/app/Activity.html瞭解有關活動生命週期的更多詳情。

  2. 如果仍然沒有幫助,請使用標記Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

  3. 嘗試使用偵聽器的上下文。無論是聽衆直接提供了一個上下文(所以你可以叫startActivity(…)或致電getApplicationContext().startActivity(…)

最終你的代碼應該是這樣的:

Intent intent = new Intent(RS_HomeScreenActivity.this,RS_HomeScreenActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
startActivity(intent); 
+0

當我開始通話時,我的活動的'onStop'方法被調用。它很好嗎? – Geek

+0

您的代碼無法使用。 – Geek

1

這是我的解決方案,它在4.3上完美工作 - 其他操作系統版本尚未測試,但一切都應該沒問題。

註冊聽衆中MainActivity

TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
    listener = new ListenToPhoneState(); 
    tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); 

的PhoneStateListener從MainActivty

private class ListenToPhoneState extends PhoneStateListener { 

    private String LOG_TAG = "mainactivity"; 
    private boolean isCallFinished = false; 

    public void onCallStateChanged(int state, String incomingNumber) { 
     if (TelephonyManager.CALL_STATE_RINGING == state) { 
      Log.i(LOG_TAG, "RINGING, number: " + incomingNumber); 
     } 
     if (TelephonyManager.CALL_STATE_OFFHOOK == state) { 
      // wait for phone to go offhook (probably set a boolean flag) so 
      // you know your app initiated the call. 
      isCallFinished = true; 
      Log.i(LOG_TAG, "OFFHOOK"); 
     } 
     if (TelephonyManager.CALL_STATE_IDLE == state) { 
      // when this state occurs, and your flag is set, restart your 
      // app 
      if (isCallFinished) { 
       isCallFinished = false; 
       Intent i = new Intent(getApplicationContext(), 
         MainActivity.class); 
       // this needs if you want some special action after the phone call 
       //ends, that is different from your normal lauch configuration 
       i.setAction("SHOW_PHONE_CALL_LIST"); 
       startActivity(i); 
       finish(); 
      } 
      Log.i(LOG_TAG, "IDLE"); 
     } 
    } 

} 

我開始從fragment的電話,但沒有任何區別:

Uri uri = Uri.parse("tel:" + Uri.encode(callIt)); 
Intent intent = new Intent(Intent.ACTION_CALL, uri); 
startActivity(intent); 
getActivity().finish(); 

您必須致電finish()關於開始通話的活動,否則在通話結束後,您的應用程序將保留默認的電話應用程序。

當你的應用程序啓動時,你可以看看intent,你可以設置你的電話後配置。在您的onCreate()方法中調用此方法:

if (intent.getAction().equals("SHOW_PHONE_CALL_LIST")) { 
     //perfom after call config here 
    } 

我希望一切都被清楚地解釋。

相關問題