how to get dialed mobile number in my application? [duplicate]

限于喜欢 提交于 2019-11-27 07:24:29

问题


Possible Duplicate:
launching my app when dialing a number

I would like to get the mobile number from dialer which has dialed by user in my android application.I have implemented an application as follows:

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

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

        }
    });

from the above code i can open dialer app.If user enter a mobile number and click for call then i would like to get which number he has typed. please any body help on it...


回答1:


You can use BroadcastReceiverto do that :

Register a BroadcastReceiver like this in Manifest file:

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

You need Permission for NEW_OUTGOING_CALL :

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

Eventually you can Receive the broadcast and retrieve the dialed number like this:

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

    }

}

}




回答2:


I got my solution as following code

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

   }
}


来源:https://stackoverflow.com/questions/9909153/how-to-get-dialed-mobile-number-in-my-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!