我正在尝试创建一个项目,其中有一个editText字段,用户将提供他的电话号码作为输入。有一个图像按钮。录音后,将拨打该号码的电话。但我无法使用用户输入 callIntent.setData(Uri.parse( “电话:XXXXXXXXXXX”));
以下是代码:
package com.example.dolphin.phonecall;
public class MainActivity extends Activity {
final Context context = this;
private ImageButton button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (ImageButton) findViewById(R.id.iButtonCall);
// add PhoneStateListener
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
// add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
//below I want to use user given phone number
callIntent.setData(Uri.parse("tel:XXXXXXXXXXX"));
if (ContextCompat.checkSelfPermission(context,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
startActivity(callIntent);
}
}
});
}
//monitor phone call activities
public class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
String LOG_TAG = "LOGGING 123";
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
Log.i(LOG_TAG, "OFFHOOK");
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended,
// need detect flag from CALL_STATE_OFFHOOK
Log.i(LOG_TAG, "IDLE");
if (isPhoneCalling) {
Log.i(LOG_TAG, "restart app");
// restart app
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
isPhoneCalling = false;
}
}
}
}
}
答案 0 :(得分:0)
你根本没有提到你的editText。
EditText userInput = (EditText) findViewById(R.id.EDIT_TEXT_ID);
String phoneNumber = userInput.getText();
并将XXXXX
替换为phoneNumber
修改:另一项建议是将XXXX..
替换为((EditText)findViewById(R.id.EDIT_TEXT_ID)).getText()