android studio拨打电话代码,AndroidStudio实现拨打电话和发短信(kotlin)
如何利用kotlin語言實現調用系統的電話和短信功能呢,其實很簡單,只要利用Intent()并且設置一下其action和data屬性即可,action代表的就是行動,如打電話就是ACTION_DIAL,發短信就是ACTION_SENDTO,然后data就是設置一些特定的屬性,如撥打的號碼,短信的內容等。
先看布局文件代碼
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
android:id="@+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打電話" />
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="發短信" />
再看Activity里面onCreate()方法的內部代碼
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//圖一所示的xml布局文件里面已經添加兩個button,一個id是call,另外一個id是message
call.setOnClickListener {
var intent = Intent()
intent.action = Intent.ACTION_DIAL//dial是撥號的意思
intent.data = Uri.parse("tel:043184978981")//這個tel不能改,后面的數字可以隨便改
startActivity(intent)
}
message.setOnClickListener {
var intent = Intent()
intent.action = Intent.ACTION_SENDTO//發短信的action
intent.data = Uri.parse("smsto:張三")//smsto:后面的是收信人,可以隨便改
intent.putExtra("sms_body", "Welcome to Android!")//這里的第二個參數是短信內容
startActivity(intent)
}
}
以上就是所有代碼啦,有興趣的可以嘗試一下~
如果有什么問題歡迎留言~有問必答
總結
以上是生活随笔為你收集整理的android studio拨打电话代码,AndroidStudio实现拨打电话和发短信(kotlin)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【C语言练习——打印上三角及其变形(带空
- 下一篇: Anaconda自带python,在cm