我用什么API /类检查拨出电话? - Android

时间:2012-07-07 15:23:37

标签: android telephonymanager

我似乎无法找到一个好的课程/ API,可以告诉我手机何时拨打电话。我知道我可以通过使用TelephonyManager来检测何时接到电话,但如何检测用户是否在呼叫某人?

以下是我目前的代码:

public class hiWorld extends Activity {

private int counter=0; 



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);




 if(tm.getCallState()==1) //ringing  
 {

     setContentView(R.layout.activity_hi_world);
     TextView tcounter = (TextView)findViewById(R.id.textbox1);

    tcounter.setText("calling");  

 }

 else if (tm.getCallState()==0) //idle 

 { 
     setContentView(R.layout.activity_hi_world);
     TextView tcounter = (TextView)findViewById(R.id.textbox1);

     tcounter.setText("not calling");
 }

1 个答案:

答案 0 :(得分:1)

您需要在应用程序中注册android.intent.action.NEW_OUTGOING_CALL接收方以接收拨出电话广播:

<小时/> 的的AndroidManifest.xml

<receiver android:name=".OutgoingCallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" >
            </action>
        </intent-filter>
    </receiver>

<小时/> 的 OutgoingCallReceiver.java:

public class OutgoingCallReceiver extends BroadcastReceiver {


     @Override
     public void onReceive(Context context, Intent intent) {

    final String originalNumber =
    intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    //START YOUR SERVICE HERE FOR COUNTING MINS
    Intent intent = new Intent(context, MyService.class);
    intent.putExtra("number", originalNumber);
    context.startService(intent);
    }
  } 

<小时/> 要求许可:

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>