Android呼叫广播接收器

时间:2014-04-09 10:52:31

标签: android broadcastreceiver

我正在尝试创建呼叫广播接收器。我已经成功创建了广播接收器服务并且还添加了清单文件。 我的程序工作正常,但在模拟器中,我可以在只使用DDMS传入号码时调用(像这样) enter image description here

现在我想在divice中运行我的应用程序。我希望editext的toString将是来电号码。所有

我该如何解决? 以下是我的代码

public class MainActivity extends Activity {


private Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b = (Button) findViewById(R.id.ok);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent in = new Intent(getApplicationContext(),
                    IncomingCall.class);
            sendBroadcast(in);

        }
    });

}



public class IncomingCall extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    TelephonyManager tmgr = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);

    MyPhoneStateListener PhoneListener = new MyPhoneStateListener();

    tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

}

private class MyPhoneStateListener extends PhoneStateListener {
    public void onCallStateChanged(int state, String incomingNumber) {


        Log.d("MyPhoneListener", state + "   incoming no:" + incomingNumber);

    }
}

}

1 个答案:

答案 0 :(得分:0)

add String number = intent.getExtras()。getString(“incoming_number”);在您收到时,此处的号码是您将收到的号码