从Android应用程序实现直接呼叫

时间:2019-01-02 02:01:46

标签: android

当用户单击按钮时,我想从我的应用程序拨打电话,但是当第一次单击该按钮时,弹出权限对话框,但是在授予权限后,启动活动未激活该调用。请帮帮我

public class MainActivity extends AppCompatActivity {
EditText et;
Button call_btn;
Context context;
private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=this;
    et=(EditText)findViewById(R.id.editText1);
    call_btn=(Button)findViewById(R.id.button1);
    call_btn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                try {
                    // TODO Auto-generated method stub
                    Intent i = new Intent(Intent.ACTION_CALL);
                    i.setData(Uri.parse("tel:" + et.getText().toString()));
                    startActivity(i);
                }
                catch (SecurityException e){
                    Writer writer = new StringWriter();
                    e.printStackTrace(new PrintWriter(writer));
                    String s = writer.toString();
                    Toast.makeText(getBaseContext(), s, Toast.LENGTH_SHORT).show();
                }
            }else {
                getPermission();
            }
        }
    });
}
public void getPermission(){
    int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_CALL_PHONE);
    } else {

    }
}

}

下面是我manifest.xml文件中的内容

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="flashcodesolution.recharger">
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <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>

1 个答案:

答案 0 :(得分:0)

您需要重写onRequestPermissionsResult方法,然后再次进行调用,如下所示:

static void IterateEnum<T>(T enumSource)
{
    foreach(var item in Enum.GetValues(typeof(T)))
    {
        Console.WriteLine(item);
    }
}

无论如何,如果您只想打开内置的Phone应用程序,则可以将操作更改为Intent.ACTION_DIAL,不需要任何权限。

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Intent i = new Intent(Intent.ACTION_CALL);
            i.setData(Uri.parse("tel:" + et.getText().toString()));
            startActivity(i);
        }
    }
}

希望有帮助。