在Android Studio中,按下呼叫按钮时输入的拨号号码将使用隐式Intent进行重定向

k97glaaz  于 2023-03-06  发布在  Android
关注(0)|答案(1)|浏览(120)

请帮助我。我在if段的MainActivity中遇到错误。

主要活动:

package com.example.practical14;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    EditText e1;
    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e1=findViewById(R.id.editTextTextPersonName);
        b=findViewById(R.id.button);
        b.setOnClickListener(this);
       }

    @Override
    public void onClick(View v)
    {
        callNumber();
    }
    private String getPhonenumber()

    {
        return e1.getText().toString();
    }
    private void callNumber()
    {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse(""+getPhonenumber()));
       if(appCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)!= PackageManager.PERMISSION_GRANTED);
        {
            return;

        }
        startActivity(intent);
    }
}

机器人清单.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.Practical14"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

日志目录:

1-

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.DIAL }

2-

error: cannot find symbol
       if(appCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)!= PackageManager.PERMISSION_GRANTED);
          ^
  symbol:   variable appCompat
  location: class MainActivity
falq053o

falq053o1#

String phone = "+34666777888";
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null));
startActivity(intent);

**注意:**此操作无需向清单添加任何内容。

我的答案来源:https://stackoverflow.com/a/24367402/19610655
如需更多答案:How to make a phone call using intent in Android?

相关问题