簡體   English   中英

電話沒有使用Intent Android_Call撥打電話

[英]Phone not making calls with Intent Android_Call

我在使用Intent Action_Call時遇到了一些麻煩。 我將權限放在Manifest中,但它不起作用。 我按下按鈕撥打電話,沒有任何反應。 我正在制作的應用程序是一個執行多個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);
}


}

來自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.
    }
}

你可以通過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);

}

您需要在運行時添加權限才能進行調用,我建議使用以下庫,因為在執行時更容易實現權限。

https://github.com/Karumi/Dexter

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM