我正在编写一个简单的应用程序,要求获得呼叫权限,用户可以在EditText中键入要拨打的电话号码,然后按“拨打电话”。
当我拨打一个真实的号码时,它最终会转到手机但是它 拨打一些长号码,例如2637643787767877.无论是否发生这种情况 我使用模拟器或我自己的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);
}
}
}
问题是,这有什么问题?
答案 0 :(得分:-1)
您没有从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);
}
}
答案 1 :(得分:-1)
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)
试试这段代码。