手机无法通过Intent Android_Call

时间:2019-04-25 19:54:58

标签: java android android-intent

我在使用Intent Action_Call时遇到麻烦。我将许可放到了清单中,但是没有用。我按按钮呼叫,但没有任何反应。我正在制作的应用程序是执行多个Intent的应用程序,因此代码不在MainActivity中。我不知道是否有帮助,但是我正在使用API​​ 28。 感谢您的阅读。

清单:

<?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)

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);
}


}

3 个答案:

答案 0 :(得分:0)

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

请求权限:

// 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
}

验证:

@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.
    }
}

答案 1 :(得分:0)

您可以通过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);

}

答案 2 :(得分:0)

您需要在运行时添加权限才能进行调用,我建议使用以下库,因为在执行时实现权限要容易得多。

https://github.com/Karumi/Dexter