2012-11-13 51 views
2

我試圖從應用回到應用程序調用

打個電話,我想它來調用

回來後返回到應用程序後,我問這個問題在這個論壇上,但我不明白的答案

How to make a phone call in android and come back to my activity when the call is done?

public void call() { 
     try { 
      Intent callIntent = new Intent(Intent.ACTION_CALL); 
      callIntent.setData(Uri.parse("tel:048598077")); 
      getContext().startActivity(callIntent); 

     } catch (ActivityNotFoundException activityException) { 
      Log.e("dialing-example", "Call failed", activityException);} 
     finally {   
      EndCallListener callListener = new EndCallListener(); 
      TelephonyManager mTM = (TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE); 
      mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE); 
     } 
    } 



    private class EndCallListener extends PhoneStateListener { 
     @Override 
     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. 
       Log.i(LOG_TAG, "OFFHOOK"); 
      } 
      if(TelephonyManager.CALL_STATE_IDLE == state) { 
       //when this state occurs, and your flag is set, restart your app 
       Log.i(LOG_TAG, "IDLE"); 
      } 
     } 

我明白這樣的一些東西,但我沒有在任何時刻開始CallListener。 那我該怎麼做呢?

回答

5

好吧,我找到了答案 這裏是代碼:

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MainActivity extends Activity { 

    final Context context = this; 
    private Button button; 

    public void onCreate(Bundle savedInstanceState) { 

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

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

     // add PhoneStateListener 


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

      @Override 
      public void onClick(View arg0) { 

       call(); 

      } 

     }); 

    } 

    private void call() 
    { 
     PhoneCallListener phoneListener = new PhoneCallListener(); 
     TelephonyManager telephonyManager = (TelephonyManager) this 
       .getSystemService(Context.TELEPHONY_SERVICE); 
     telephonyManager.listen(phoneListener, 
       PhoneStateListener.LISTEN_CALL_STATE); 

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

    } 

    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 
        Intent i = getBaseContext().getPackageManager() 
          .getLaunchIntentForPackage(
            getBaseContext().getPackageName()); 
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(i); 

        isPhoneCalling = false; 
       } 

      } 
     } 
    } 
} 

,以及我們需要添加

<uses-permission android:name="android.permission.CALL_PHONE" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
1

我真的不知道你的應用程序應該如何工作,但如果你在文本視圖中有你的電話號碼,只需設置(在你的.xml文件中)屬性:android:autoLink="phone",一切都會正常工作。 (在點擊一下,你就可以撥打電話,當你打到底調用它會返回到你的活動)

編輯:如果你想自動執行它,那麼你應該使用intent.try此: how to make phone call using intent in android?

+0

我把它在manifest.xml和它仍然stais的通話記錄 – igor

+0

你不需要把它在manifest.xml的manufest。把它放在哪裏,你有一個包含要call.Here數文本視圖.xml文件你有一個例子:

+0

我在做對吧? 主.XML JAVA FILE private void call(){ try {intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse(「tel:xxxxxxx」)); startActivity(callIntent); (「Dialing-example」,「Call failed」,activityException); } – igor