如何在响铃时检索来电号码并将其存储在android中的变量中?

时间:2011-04-06 18:30:12

标签: java android

我对Android很新,我希望我的应用程序能够在响铃和存储时检索来电者的电话号码。我怎样才能做到这一点?

5 个答案:

答案 0 :(得分:2)

对不起,我不能给出更详细的答案,但请查看this project.

它使用AIDL与ITelephony接口进行通信。这应该从正确的方向开始。

答案 1 :(得分:1)

您需要使用BroadcastReceiver。看起来应该是这样的:

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

    }
}

答案 2 :(得分:0)

答案 3 :(得分:0)

需要扩展BroadcastReceiver

公共类CallReceiver扩展了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);
                }
        }
}

答案 4 :(得分:0)

您应该注册广播接收器并获取电话状态并获取来电号码:

public class CallReceiver extends BroadcastReceiver {
    String state,number,message;
    @Override
    public void onReceive(Context context, Intent intent) {
        state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
            number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            message = "phone is ringing";
            Toast.makeText(context, "Incoming Call From:"+number, Toast.LENGTH_SHORT).show();
        }
        if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))){
            Toast.makeText(context, "Call Received", Toast.LENGTH_SHORT).show();
        }
        if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
            message += "phone is idled";
            Toast.makeText(context, "Idled", Toast.LENGTH_SHORT).show();
        }
    }
}