Android 自动拨打电话功能可以通过以下步骤实现:
1. 在 AndroidManifest.xml 文件中添加拨打电话的权限:
```
<uses-permission android:name="android.permission.CALL_PHONE" />
```
n2. 在代码中使用 Intent 启动拨打电话的界面:
```
String phoneNumber = "10086";
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
startActivity(intent);
```
注意:使用 ACTION_CALL 需要在 AndroidManifest.xml 文件中添加以下权限:
```
<uses-permission android:name="android.permission.CALL_PHONE" />
```
如果不想直接拨打电话,而是想跳转到拨号界面,可以使用 ACTION_DIAL:
```
String phoneNumber = "10086";
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
startActivity(intent);
```
这样就可以实现 Android 自动拨打电话功能了。
完整代码:
public class MainActivity extends AppCompatActivity {private EditText etPhone;private TextView tvTime;private final static String simSlotName[] = {"extra_asus_dial_use_dualsim","com.android.phone.extra.slot",//基本上是这个"slot","simslot","sim_slot","subscription",//华为是这个"Subscription","phone","com.android.phone.DialingMode","simSlot","slot_id",//小米是这个"simId","simnum","phone_type","slotId","slotIdx"};private Handler mHandler = new Handler();private Runnable runnable;private int time = 5;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);etPhone = findViewById(R.id.et_phone);tvTime = findViewById(R.id.tv_time);TextView tvCall1 = findViewById(R.id.tv_call1);TextView tvCall2 = findViewById(R.id.tv_call2);TextView tvHangUp = findViewById(R.id.tv_hang_up);if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {//未授权ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);} else {//已授权}TelephonyManager tpm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);tpm.listen(new MyPhontStateListener(), PhoneStateListener.LISTEN_CALL_STATE);runnable = new Runnable() {@Overridepublic void run() {time--;if (time == 0) {time = 5;String phone = etPhone.getText().toString();try {/*** Intent.ACTION_CALL 直接拨打电话* Intent.ACTION_DIAL 跳转到拨号盘*/Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);for (int i = 0; i < simSlotName.length; i++) {intent.putExtra(simSlotName[i], 0);}startActivity(intent);Log.e("错误信息:", "跳转");} catch (Exception e) {Log.e("错误信息:", e.getMessage());}} else {tvTime.setText(time + "");mHandler.postDelayed(this, 1000);}}};tvCall1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {time = 5;tvTime.setText("5");mHandler.postDelayed(runnable, 500);}});tvCall2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String phone = etPhone.getText().toString();try {/*** Intent.ACTION_CALL 直接拨打电话* Intent.ACTION_DIAL 跳转到拨号盘*/Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);for (int i = 0; i < simSlotName.length; i++) {intent.putExtra(simSlotName[i], 1);}startActivity(intent);} catch (Exception e) {Log.e("错误信息:", e.getMessage());}}});tvHangUp.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {}});}@Overrideprotected void onRestart() {super.onRestart();time = 5;tvTime.setText("5");mHandler.postDelayed(runnable, 500);}@Overrideprotected void onDestroy() {mHandler.removeCallbacks(runnable);mHandler = null;super.onDestroy();}class MyPhontStateListener extends PhoneStateListener {@Overridepublic void onCallStateChanged(int state, String phoneNumber) {Log.e("电话状态:", "------------");switch (state) {case TelephonyManager.CALL_STATE_IDLE://空闲Log.e("电话状态:", "空闲");break;case TelephonyManager.CALL_STATE_RINGING://来电Log.e("电话状态:", "来电");break;case TelephonyManager.CALL_STATE_OFFHOOK://正在通话中Log.e("电话状态:", "正在通话中");break;}}}
}
个人中心
DownLoad