我为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/