java 如何在振铃时检索来电号码并将其存储在 android 中的变量中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5571249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do i retrieve the incoming phone call's number while ringing and store it in a variable in android?
提问by waa1990
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?
我对 android 还很陌生,我希望我的应用程序能够在响铃时检索来电者的电话号码并存储它。我怎样才能做到这一点?
回答by malandro95
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);
}
}
回答by YOGESH UKALE
Need to Extend BroadcastReceiver
需要扩展 BroadcastReceiver
public class CallReceiver extends 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);
}
}
}