how do i retrieve the incoming phone call's nu

2020-02-29 11:59发布

I am fairly new to android and I would like my app to be able to retrieve the phone number of caller while ringing and store it. How can I do this?

标签: java android
4条回答
甜甜的少女心
2楼-- · 2020-02-29 12:20

Need to Extend BroadcastReceiver

public class CallReceiver extends BroadcastReceiver {

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

        try {
            String state =intent.getStringExtra(TelephonyManager.EXTRA_STATE);             

             String number=intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

                }

             catch (Exception e) {
                    Log.e(TAG," Exception "+e);
                }
        }
}
查看更多
对你真心纯属浪费
3楼-- · 2020-02-29 12:26

You need to use a BroadcastReceiver. It should look something like this:

public class CallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
        Intent i = new Intent(context, IncomingCallPopup.class);
        i.putExtras(intent);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        context.startActivity(i);

    }
}
查看更多
Animai°情兽
5楼-- · 2020-02-29 12:31

I'm sorry I can't give a more detailed answer, but check out this project.

It makes use of an AIDL to communicate with the ITelephony interface. This should get started in the right direction.

查看更多
登录 后发表回答