2016-12-15 15 views
0

我正在从Android Studio发起通话。代码如下: 我想在任何时候获得通话状态。链接:https://developer.android.com/reference/android/telecom/Call.html 显示调用状态可以通过使用Class调用获得。如果我使用Call.getState(),我应该能够获得当前状态。但是我收到编译错误: 错误:(28,20)错误:Call()在调用中不公开;不能从外部包访问。在枚举中定义了几种呼叫状态:拨号,振铃,连接,DI连接,保持等。 当我运行代码时,它确实进行了呼叫,因为我可以看到模拟器拨打电话的屏幕。如何获取即将离任的Android通话的通话状态

开发者指南没有提供使用这些类的任何示例。 谢谢你的帮助。

package com.example.ramesh.makeacall; 

import android.app.Activity; 
import android.content.ActivityNotFoundException; 
import android.content.Intent; 
import android.net.Uri; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.telecom.Call; 
import android.telephony.*; 

import android.util.Log; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Call call; 
     call = new Call(); 
     call(); 

    } 
    private void call() { 

     try { 

      Intent callIntent = new Intent(Intent.ACTION_CALL); 
      callIntent.setData(Uri.parse("tel:5555551212")); 
      System.out.println("====before startActivity===="); 
      startActivity(callIntent); 


     } catch (ActivityNotFoundException e) { 
      Log.e("helloAndroid","Call failed",e); 
     } 
    } 

    } 

回答

0

尝试使用这样的(还没有尝试过,虽然) -

Call.Callback callback = new Call.Callback() { 
    @Override 
    public void onStateChanged(Call call, int state) { 
     super.onStateChanged(call, state); 
     if(state == Call.STATE_RINGING){ 
      //you code goes here 
     } 
    } 
}; 
+0

谢谢..它的工作原理 –

0
public class MyPhoneStateListener extends PhoneStateListener { 
    @Override 
    public void onCallStateChanged(int state, String incomingNumber) { 
     switch (state) { 
      case TelephonyManager.CALL_STATE_RINGING: 
       handleRinging(incomingNumber); 
       break; 
      case TelephonyManager.CALL_STATE_OFFHOOK: 
       handleOffHook(); 
       break; 
      case TelephonyManager.CALL_STATE_IDLE: 
       handleIdle(); 
       break; 
     } 
     super.onCallStateChanged(state, incomingNumber); 
    } 
} 

和寄存器statelistener:

telephonyManager =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(myPhoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);

+0

感谢。上面的代码只给出3个呼叫状态。我正在尝试按照以下定义使用类Call:https://developer.android.com/reference/android/telecom/Call.html。这给出了几个状态,如CALL_ACTIVE,CALL_DISCONNECTED,CALL_HOLD等。我怎样才能使用Call()类? –

+0

好的。根据我的理解,因为Call Class不是按照android文档公开的,所以不能使用Call类。有没有办法绕过它,这样除了上面提到的3之外,我可以获得通话的当前状态? –