如何在安卓工作室中的按钮的帮助下拨打编辑文本中给出的电话号码



>我需要拨打我在edittext中指定的给定电话号码。但是当我输入号码后按下呼叫按钮时,没有任何响应。

edittext中输入号码后,我需要在按下呼叫按钮后立即从我的应用程序启动呼叫者窗口。朋友们,请帮助我。并让我知道我的代码中出了什么问题。

非常感谢您在这件事上的时间和帮助。

这是我的编码

主活动.java:

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    EditText number;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button mDialButton = (Button) findViewById(R.id.btn_dial);
        mDialButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                callNumber();
            }
        });
    }
    private String getPhoneNumber() {
        number = (EditText) findViewById(R.id.number);
        return number.getText().toString();
    }
    private void callNumber() {
        Intent call = new Intent(Intent.ACTION_CALL);
        call.setData(Uri.parse("tel:" + getPhoneNumber()));
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        startActivity(call);
    }
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="arun.com.phonecall">
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

您尚未请求权限。如果您还没有权限,则需要请求这些权限,而不是直接返回。

您可以使用我的库轻松处理运行时权限。检查 https://github.com/nabinbhandari/Android-Permissions

问题可能与权限有关,因为(从您的代码中)您没有正确请求权限ACTION_CALL阅读更多有关在运行时请求权限中请求权限的权限。

如果您不想拥有以下权限的业务,可以使用ACTION_DIAL

Intent intent = new Intent(Intent.ACTION_DIAL, 
                       Uri.fromParts("tel", getPhoneNumber(), null));

试试这段代码

public class MainActivity extends AppCompatActivity {
EditText number;
int REQUEST_PHONE_CALL = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button mDialButton = (Button) findViewById(R.id.btn_dial);
    mDialButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            callNumber();
        }
    });
}
private String getPhoneNumber() {
    number = (EditText) findViewById(R.id.number);
    return number.getText().toString();
}
private void callNumber() {
    Intent call = new Intent(Intent.ACTION_CALL);
    call.setData(Uri.parse("tel:" + getPhoneNumber()));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
            && checkSelfPermission(
            Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(
                new String[]{
                        Manifest.permission.CALL_PHONE},
                REQUEST_PHONE_CALL);
    }
    else
    {
        startActivity(call);
    }
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case 1000:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Intent call = new Intent(Intent.ACTION_CALL);
                call.setData(Uri.parse("tel:" + getPhoneNumber()));
                startActivity(call);
            } else {
                Toast.makeText(MainActivity.this, "Permission is denied", Toast.LENGTH_SHORT).show();
            }
            break;
    }
}

}
            Intent call = new Intent(Intent.ACTION_CALL);
            call .setData(Uri.parse("tel:" +  getPhoneNumber()));
            call .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (ActivityCompat.checkSelfPermission(this, 
            Manifest.permission.CALL_PHONE) != 
            PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            startActivity(call);

首先检查Manifest.permission.CALL_PHONE的运行时权限,然后你可以像这样调用

      Intent intent = new Intent(Intent.ACTION_DIAL);
      intent.setData(Uri.parse("tel:" + getPhoneNumber()));
      startActivity(intent); 

最新更新