如何在我的申请中获得拨打的手机号码?

时间:2012-03-28 14:02:58

标签: android phone-number phone-call

  

可能重复:
  launching my app when dialing a number

我想从我的Android应用程序中用户拨打的拨号器中获取手机号码。我已经实现了如下应用程序:

 ((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
             startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:")));

        }
    });

从上面的代码我可以打开拨号器app。如果用户输入手机号码并点击呼叫,那么我想知道他输入了哪个号码。 请任何身体帮助...

2 个答案:

答案 0 :(得分:10)

您可以使用BroadcastReceiverto执行此操作:

在Manifest文件中注册这样的BroadcastReceiver:

    <!-- DIAL Receiver -->
    <receiver
        android:exported="true"
        android:name="receivers.DialBroadcastReceiver" >
        <intent-filter >
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

您需要NEW_OUTGOING_CALL的权限:

<!-- OUTGOING CALL PERMISSION-->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

最终您可以接收广播并检索拨打的号码,如下所示:

public class DialBroadcastReceiver extends BroadcastReceiver {

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

    Log.v("DileBroadCastReceiver","In onReceive()");

    if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
         String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

         Log.v("DialBroadcast Receiver","Number is: "+number);

    }

}

}

答案 1 :(得分:0)

我的解决方案如下代码

public class OutGoingCall extends BroadcastReceiver {

   @Override
   public void onReceive(final Context context, final Intent intent) 
   {
         // get phone number from bundle
         String phoneNumber = intent.getExtras().getString(OutGoingCall.INTENT_PHONE_NUMBER);

   }
}