[英]how do i retrieve the incoming phone call's number while ringing and store it in a variable in android?
我是 android 的新手,我希望我的應用程序能夠在響鈴時檢索來電者的電話號碼並存儲它。 我怎樣才能做到這一點?
您需要使用 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);
}
}
需要擴展 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);
}
}
}
您應該注冊廣播接收器並獲取電話狀態並獲取來電號碼:
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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.