如何确定在一个独立的应用程序的电流呼叫者的电话号码 [英] How to determine the phone number of a current caller in a stand-alone application
问题描述
我想建立一个Android应用程序,可以通过pre-确定短信联系当前调用者。发送文本消息是很简单的,但在确定一个独立的应用程序的当前呼叫方的电话号码是一个挑战。是有一个简单的方法就可以洞察到的电话号码,所以我仍然可以给他们发送消息,而在电话吗?
当然,也有手工的方式来做到这一点:记下数目,钥匙插入一个新的文本信息,输入信息。但我想定义消息了前面,能够将其发送给当前调用者。
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
TelephonyManager电话=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneCallStateListener customPhoneListener =新PhoneCallStateListener(上下文);
telephony.listen(customPhoneListener,PhoneStateListener.LISTEN_CALL_STATE);
辅助=新ContactDatabaseHelper(上下文);
表= helper.getAllContacts();
尝试{
incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
如果(则为list.size()!= 0){
的for(int i = 0,大小=则为list.size(); I<大小;我++){
如果(PhoneNumberUtils.compare(incomingNumber,list.get(ⅰ).getContactNumber())){
ToastMsg.showToast(背景下,list.get(我).getContactName()+调用);
}
}
}
}赶上(例外五){
// TODO:处理异常
}
}
公共类PhoneCallStateListener扩展PhoneStateListener {
私人上下文的背景下;
公共PhoneCallStateListener(上下文的背景下){
this.context =背景;
}
@覆盖
公共无效onCallStateChanged(INT状态,串incomingNumber){
开关(州){
案例TelephonyManager.CALL_STATE_RINGING:
打破;
案例PhoneStateListener.LISTEN_CALL_STATE:
}
super.onCallStateChanged(州,incomingNumber);
}
}
I'd like to build an Android application that can contact the current caller via a pre-determined text message. Sending a text message is simple enough but determining the phone number of the current caller in a stand-alone application is the challenge. Is the there an easy way to divine the phone number so I can send them a message while still on the call?
Of course there are manual ways to do this: write down the number, key it into a new text message, enter the message. But I want to define the message up front and be able to "send it to current caller".
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneCallStateListener customPhoneListener = new PhoneCallStateListener(context);
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
helper = new ContactDatabaseHelper(context);
list = helper.getAllContacts();
try{
incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (list.size() != 0){
for ( int i = 0, size = list.size(); i < size; i++ ){
if (PhoneNumberUtils.compare(incomingNumber, list.get(i).getContactNumber())){
ToastMsg.showToast(context,list.get(i).getContactName()+" Calling");
}
}
}
}catch (Exception e) {
// TODO: handle exception
}
}
public class PhoneCallStateListener extends PhoneStateListener{
private Context context;
public PhoneCallStateListener(Context context){
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
break;
case PhoneStateListener.LISTEN_CALL_STATE:
}
super.onCallStateChanged(state, incomingNumber);
}
}
这篇关于如何确定在一个独立的应用程序的电流呼叫者的电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!