PhoneCall - 拨打奇怪的长号码而不是输入的号码
PhoneCall - Dials strange long number instead of the one entered
我正在编写一个简单的应用程序,请求通话权限,用户可以在 EditText 中输入他们想要拨打的 phone 号码,然后按“拨打电话”。
When I dial in a real number, it ends up going to the phone but it
dials some long number such as 2637643787767877. This happens whether
I use the emulator or my own galaxy s6.
我的清单文件中当然有这个:
<uses-permission android:name="android.permission.CALL_PHONE" />
这是我的代码。
package cornez.com.phonecall;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText phoneNumToCall;
Button makeCallButton;
int permissionCheck;
private static final int PERMISSION_REQUEST_CALL_PHONE = 100;
private static final String LOG_TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phoneNumToCall = (EditText) findViewById(R.id.editText);
makeCallButton = (Button) findViewById(R.id.button);
//onclicklistener for BUTTON
permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.CALL_PHONE);
makeCallButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
makeCall();
}
else {
// request the permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CALL_PHONE},
PERMISSION_REQUEST_CALL_PHONE);
}
} });
//makeCallButton.setOnClickListener();
phoneNumToCall.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.
SOFT_INPUT_STATE_VISIBLE);
}
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[],
@NonNull int[] grantResults)
{
if (requestCode == PERMISSION_REQUEST_CALL_PHONE)
{
if (grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
makeCall();
}
else
{
// notify user that permission was denied
Toast.makeText(this, "The permission was denied", Toast.LENGTH_SHORT).show();
}
}
}
private void makeCall()
{
try
{
Uri uri = Uri.parse("tel:" + phoneNumToCall);
Intent intent = new Intent(Intent.ACTION_CALL, uri);
startActivity(intent);
}
catch (SecurityException ex)
{
String errorMsg = "No permission to make phone call.";
Log.e(LOG_TAG, errorMsg, ex);
}
}
}
问题是,这有什么问题?
您没有解析来自 EditText
的字符串
您只需声明 EditText 并调用它。
phoneNumToCall = (EditText) findViewById(R.id.editText);
试试这个
用这个
替换makeCall()
方法
private void makeCall()
{
try
{
Uri uri = Uri.parse("tel:" + phoneNumToCall.getText().toString());
Intent intent = new Intent(Intent.ACTION_CALL, uri);
startActivity(intent);
}
catch (SecurityException ex)
{
String errorMsg = "No permission to make phone call.";
Log.e(LOG_TAG, errorMsg, ex);
}
}
String uri = "tel:" + callNumber;
String deviceName = android.os.Build.MODEL;
Intent intent;
if ("Nexus 5".equalsIgnoreCase(deviceName) || "XT1092".equalsIgnoreCase(deviceName) || "XT1572".equalsIgnoreCase(deviceName) || "XT1575".equalsIgnoreCase(deviceName) ) {
intent = new Intent(Intent.ACTION_DIAL);
} else {
intent = new Intent(Intent.ACTION_CALL);
}
intent.setData(Uri.parse(uri));
startActivity(intent)
试试这个代码。
我正在编写一个简单的应用程序,请求通话权限,用户可以在 EditText 中输入他们想要拨打的 phone 号码,然后按“拨打电话”。
When I dial in a real number, it ends up going to the phone but it dials some long number such as 2637643787767877. This happens whether I use the emulator or my own galaxy s6.
我的清单文件中当然有这个:
<uses-permission android:name="android.permission.CALL_PHONE" />
这是我的代码。
package cornez.com.phonecall;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText phoneNumToCall;
Button makeCallButton;
int permissionCheck;
private static final int PERMISSION_REQUEST_CALL_PHONE = 100;
private static final String LOG_TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phoneNumToCall = (EditText) findViewById(R.id.editText);
makeCallButton = (Button) findViewById(R.id.button);
//onclicklistener for BUTTON
permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.CALL_PHONE);
makeCallButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
makeCall();
}
else {
// request the permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CALL_PHONE},
PERMISSION_REQUEST_CALL_PHONE);
}
} });
//makeCallButton.setOnClickListener();
phoneNumToCall.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.
SOFT_INPUT_STATE_VISIBLE);
}
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[],
@NonNull int[] grantResults)
{
if (requestCode == PERMISSION_REQUEST_CALL_PHONE)
{
if (grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
makeCall();
}
else
{
// notify user that permission was denied
Toast.makeText(this, "The permission was denied", Toast.LENGTH_SHORT).show();
}
}
}
private void makeCall()
{
try
{
Uri uri = Uri.parse("tel:" + phoneNumToCall);
Intent intent = new Intent(Intent.ACTION_CALL, uri);
startActivity(intent);
}
catch (SecurityException ex)
{
String errorMsg = "No permission to make phone call.";
Log.e(LOG_TAG, errorMsg, ex);
}
}
}
问题是,这有什么问题?
您没有解析来自 EditText
的字符串
您只需声明 EditText 并调用它。
phoneNumToCall = (EditText) findViewById(R.id.editText);
试试这个
用这个
替换makeCall()
方法
private void makeCall()
{
try
{
Uri uri = Uri.parse("tel:" + phoneNumToCall.getText().toString());
Intent intent = new Intent(Intent.ACTION_CALL, uri);
startActivity(intent);
}
catch (SecurityException ex)
{
String errorMsg = "No permission to make phone call.";
Log.e(LOG_TAG, errorMsg, ex);
}
}
String uri = "tel:" + callNumber;
String deviceName = android.os.Build.MODEL;
Intent intent;
if ("Nexus 5".equalsIgnoreCase(deviceName) || "XT1092".equalsIgnoreCase(deviceName) || "XT1572".equalsIgnoreCase(deviceName) || "XT1575".equalsIgnoreCase(deviceName) ) {
intent = new Intent(Intent.ACTION_DIAL);
} else {
intent = new Intent(Intent.ACTION_CALL);
}
intent.setData(Uri.parse(uri));
startActivity(intent)
试试这个代码。