Activity
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var isOpenRepeat = true
private lateinit var telephonyManager: TelephonyManager
private var intervalCall by Delegates.notNull<Int>()
private var repeatCount by Delegates.notNull<Int>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
initListener()
}
private fun initListener() {
binding.btnCall.setOnClickListener {
if (TextUtils.isEmpty(binding.etPhone.text.toString())) {
Toast.makeText(this, "请输入号码", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
initPermission()
}
binding.switchRepeat.setOnCheckedChangeListener { buttonView, isChecked ->
isOpenRepeat = isChecked
if (isChecked) {
binding.etRepeatCountLayout.visibility = View.VISIBLE
binding.switchRepeat.text = "已开启重复拨打"
} else {
binding.etRepeatCountLayout.visibility = View.GONE
binding.switchRepeat.text = "已关闭重复拨打"
}
}
binding.etRepeatCount.addTextChangedListener {
repeatCount = it.toString().toInt()
}
binding.etInterval.addTextChangedListener {
intervalCall = it.toString().toInt()
}
}
private fun initPermission() {
PermissionX.init(this)
.permissions(Manifest.permission.CALL_PHONE)
.explainReasonBeforeRequest()
.onForwardToSettings { scope, deniedList ->
scope.showForwardToSettingsDialog(deniedList, "您需要去应用程序设置当中手动开启权限才能使用", "我已明白")
}
.request { allGranted, _, _ ->
if (allGranted) {
callPhone()
}
}
}
private fun callPhone() {
val intent = Intent(Intent.ACTION_CALL)
val data = Uri.parse("tel:" + binding.etPhone.text.toString())
intent.data = data
startActivity(intent)
}
}
布局
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@drawable/callphone_bg"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/et_repeat_count_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="10dp"
android:theme="@style/InputTheme"
app:hintTextAppearance="@style/InputHintTheme"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/switch_repeat"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/et_repeat_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入重复拨打次数"
android:inputType="number"
android:text="9999"
android:textColor="@color/white" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/switch_repeat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginRight="10dp"
android:checked="true"
android:theme="@style/SwitchTheme"
android:textColor="@color/white"
android:text="已开启重复拨打"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/et_interval_layout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginLeft="36dp"
android:layout_marginTop="260dp"
android:layout_marginRight="36dp"
app:hintTextAppearance="@style/InputHintTheme"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/et_interval"
android:layout_width="match_parent"
android:theme="@style/InputTheme"
android:inputType="number"
android:text="1"
android:layout_height="wrap_content"
android:hint="请输入拨打间隔时间"
android:textColor="@color/white" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/et_phone_layout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginLeft="36dp"
android:layout_marginRight="36dp"
app:hintTextAppearance="@style/InputHintTheme"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_interval_layout">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:theme="@style/InputTheme"
android:layout_height="wrap_content"
android:hint="请输入手机号"
android:textColor="@color/white" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/btn_call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="36dp"
android:layout_marginTop="20dp"
android:layout_marginRight="36dp"
android:backgroundTint="@color/teal_700"
android:text="开始拨打"
android:textColor="@color/white"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_phone_layout" />
</androidx.constraintlayout.widget.ConstraintLayout>
样式
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Theme.CallPhone" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
<style name="InputTheme">
<item name="colorControlNormal">@color/white</item>
<item name="colorControlActivated">@color/white</item>
<item name="colorControlHighlight">@color/white</item>
</style>
<style name="InputHintTheme" parent="Base.TextAppearance.AppCompat.Caption">
<item name="android:textColor">@color/white</item>
</style>
<style name="SwitchTheme">
<item name="colorControlActivated">#073763</item>
<item name="colorSwitchThumbNormal">@color/white</item>
<item name="android:colorForeground">@color/white</item>
</style>
</resources>
广播
class PhoneBroadcastReceiver : BroadcastReceiver() {
private lateinit var telephonyManager: TelephonyManager
companion object {
var mIncomingFlag = false;
var callOver = false;
}
override fun onReceive(context: Context, intent: Intent) {
Log.e("拨打","注册广播")
if (intent.action === Intent.ACTION_NEW_OUTGOING_CALL) {
telephonyManager = context.getSystemService(Service.TELEPHONY_SERVICE) as TelephonyManager
when(telephonyManager.callState) {
TelephonyManager.CALL_STATE_IDLE -> {
Log.e("拨打","空闲中")
}
TelephonyManager.CALL_STATE_OFFHOOK -> {
Log.e("拨打","接听中")
}
TelephonyManager.CALL_STATE_RINGING -> {
Log.e("拨打","响铃中")
}
}
}
}
}