【问题标题】:how to get dialed mobile number in my application? [duplicate]如何在我的应用程序中获取拨打的手机号码? [复制]
【发布时间】:2012-04-12 03:24:48
【问题描述】:

可能重复:
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:")));

        }
    });

从上面的代码我可以打开拨号应用程序。如果用户输入手机号码并点击通话,那么我想得到他输入的号码。 请任何人帮忙...

【问题讨论】:

  • 我不同意关闭此线程 - “重复”根本没有讨论获取电话号码。此外,答案的价值要低得多。如果您想关闭一个线程,请关闭另一个。
  • 我也在尝试这样的事情。但是我想在拨号器应用程序中拨出某些东西后立即在拨打电话之前获取号码。任何身体都可以帮忙吗?

标签: android phone-number phone-call


【解决方案1】:

我的解决方案如下代码

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);

   }
}

【讨论】:

  • Intent i=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + phoneno));
  • 你也可以这样做 Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:"+pnmu)); context.startActivity(callIntent);通过变量发送号码
【解决方案2】:

您可以使用 BroadcastReceiver 来做到这一点:

在 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);

    }

}

}

【讨论】:

  • PROCESS_OUTGOING_CALLS 被禁止(2019 年 1 月),因为该应用不是默认的电话呼叫处理程序
猜你喜欢
  • 1970-01-01
  • 2023-04-10
  • 2020-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-08
  • 2014-05-09
相关资源
最近更新 更多