简体   繁体   English

电话没有使用Intent Android_Call拨打电话

[英]Phone not making calls with Intent Android_Call

I'm having some trouble with Intent Action_Call. 我在使用Intent Action_Call时遇到了一些麻烦。 I put the permission in Manifest, but it doesn't work. 我将权限放在Manifest中,但它不起作用。 I press the button to Call and nothing happens. 我按下按钮拨打电话,没有任何反应。 The app that I'm making is an app that does multiple Intents so the code isn't in MainActivity. 我正在制作的应用程序是一个执行多个Intent的应用程序,因此代码不在MainActivity中。 I don't know if it helps, but I'm using API 28. Thanks for reading. 我不知道它是否有帮助,但我正在使用API​​ 28.感谢阅读。

MANIFEST: 表现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intentsimplicitas">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />

<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=".SmsActivity"></activity>
    <activity android:name=".DialActivity" />
    <activity android:name=".WaysActivity" />
    <activity android:name=".MapActivity" />
    <activity android:name=".PageActivity" />
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

JAVA (DialActivity.java) JAVA(DialActivity.java)

public class DialActivity extends Activity {

Button btnDial;
EditText edtPhone;
String phone;
Intent it;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dial);
    btnDial = (Button)findViewById(R.id.btnDial);
    edtPhone = (EditText)findViewById(R.id.edtPhone);
}

public void dialClick (View v) {
        phone = edtPhone.getText().toString();
        Uri uri = Uri.parse("tel: " + phone);
        it = new Intent(Intent.ACTION_CALL);
        it.setData(uri);
        startActivity(it);
}


}

From https://developer.android.com/training/permissions/requesting : 来自https://developer.android.com/training/permissions/requesting

Requesting permission: 请求许可:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.CALL_PHONE)
        != PackageManager.PERMISSION_GRANTED) {

    // Permission is not granted
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.CALL_PHONE)) {
        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.
    } else {
        // No explanation needed; request the permission
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.CALL_PHONE},
                MY_PERMISSIONS_REQUEST_CALL_PHONE);

        // MY_PERMISSIONS_REQUEST_CALL_PHONE is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
} else {
    // Permission has already been granted
}

Verifying: 验证:

@Override
public void onRequestPermissionsResult(int requestCode,
        String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permission was granted, yay! Do the
                // contacts-related task you need to do.
            } else {
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request.
    }
}

You can do via Intent 你可以通过Intent来做

public void dialClick (View v) {
    phone = edtPhone.getText().toString();
    Uri uri = Uri.parse("tel: " + phone);
    it = new Intent(Intent.ACTION_DIAL);
    it.setData(uri);
    startActivity(it);

} }

You need to add the permissions at run time to be able to make the call, I recommend the following library, since it is much easier to implement the permissions in time of execution. 您需要在运行时添加权限才能进行调用,我建议使用以下库,因为在执行时更容易实现权限。

https://github.com/Karumi/Dexter https://github.com/Karumi/Dexter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM