android - 电话API无效

标签 android kotlin gradle

我为android开发了一个简单的电话应用程序,我在android studio上的Android Manifest文件中做了Permission internet,我使用这种方法使我的应用程序可用于调用android.Manifest.permission.CALL_PHONE
但是当我运行我的代码时,什么也没发生
这是头等舱:

class MainActivity : AppCompatActivity() {

    val mobno: String = "1234567890"
    val REQUEST_PHONE_CALL = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val call = findViewById<Button>(R.id.phone_call)

        call.setOnClickListener {
            if (ActivityCompat.checkSelfPermission(
                    this,
                    android.Manifest.permission.CALL_PHONE
                ) != PackageManager.PERMISSION_GRANTED
            ) {

            }
        }
    }

    private fun makecall() {
        val intent = Intent(Intent.ACTION_CALL, Uri.fromParts("tel", mobno, null))
        startActivity(intent)
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        if (requestCode == REQUEST_PHONE_CALL) {
            makecall()
        }
    }
}
这是我的main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/phone_call"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        android:text="Place Call" />
</LinearLayout>
这是我的 list :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.telephoneappkotlin">

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


    <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>
在我的毕业典礼上,我不使用任何库实现。

最佳答案

将此添加到 list 中

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

关于android - 电话API无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64564145/

相关文章:

android - 如何查看是否有后台任务在运行?

android - 打瞌睡模式处理

android - "Gradle files have changed since last project sync."消息总是出现

java - gradle包括另一个项目的库

android - 这是检查用户位置(android应用程序)是否在多边形内的更好方法

c# - 存档的 APK 文件大小非常巨大 - Xamarin.Android

java - 安卓刀柄。是否可以将一个 ViewModel 注入(inject)另一个 ViewModel 中?

IntelliJ 中的 Kotlin 多平台 JVM 类型不匹配

kotlin - 如何避免 "?.let"的嵌套「空检查」?

android - 错误 :Execution failed for task ':app:compileDebugKotlin' . > 编译错误。有关详细信息,请参阅日志