【Android】用Android写个自己的打电话APP

608 阅读1分钟

本文已参加[新人创作礼]活动,一起开启掘金创作之路 ​

 环境:Android-studio

Android 6

1.常规操作,布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="29dp"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_alignParentLeft="true"
        android:onClick="makeCall"
        android:text="Call" />

    <TextView
        android:id="@+id/textShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="45dp"
        android:text="CellPhone Message" />

</RelativeLayout>

2.清单文件给权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dadianhua">
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <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/Theme.Map">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

3.主活动

package com.example.dadianhua;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText phoneNumber;
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        phoneNumber = findViewById(R.id.editText1);
        tv = findViewById(R.id.textShow);

        TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        int phoneType = tel.getPhoneType();
        String phoneNetType = "";
        switch (phoneType){
            case TelephonyManager.PHONE_TYPE_CDMA:
                phoneNetType = "CDMA";
                break;
            case TelephonyManager.PHONE_TYPE_GSM:
                phoneNetType = "GSM";
                break;
            case TelephonyManager.PHONE_TYPE_NONE:
                phoneNetType = "NONE";
                break;
            case TelephonyManager.PHONE_TYPE_SIP:
                phoneNetType = "SIP";
                break;
            default:
                break;
        }
        StringBuilder info = new StringBuilder("Phone Details:");
        info.append("\n Network Country ISO: "+tel.getNetworkCountryIso());
        info.append("\n Phone Network Type: "+phoneNetType);
        info.append("\n Network operator Name: "+tel.getNetworkOperatorName());
        info.append("\n Network operator: "+tel.getNetworkOperator());
        info.append("\n Sim Operator Name: "+tel.getSimOperatorName());
        tv.setText(info);
        //电话状态监听
        PhoneStateListener callStateListener = new PhoneStateListener(){
            @Override
            public void onCallStateChanged(int state, String phoneNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING){
                    Toast.makeText(getApplicationContext(), "电话已接通", Toast.LENGTH_SHORT).show();
                }
                if (state == TelephonyManager.CALL_STATE_OFFHOOK){
                    Toast.makeText(getApplicationContext(), "正在通话中", Toast.LENGTH_SHORT).show();
                }
                if (state == TelephonyManager.CALL_STATE_IDLE){
                    Toast.makeText(getApplicationContext(), "电话没通,而没人打电话", Toast.LENGTH_SHORT).show();
                }
            }
        };
        tel.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);

    }
    public void makeCall(View view){
        String myNum = phoneNumber.getText().toString();
        startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+myNum))
        );
    }
}

\

​​

 4.运行吧,给权限

 5.运行