0

我似乎找不到一个好的类/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");
 }
4

1 回答 1

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"/>

于 2012-07-07T15:28:40.643 回答