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

【Android】用Android写个自己的打电话APP,第1张

环境:Android-studio

Android 6

1.常规 *** 作,布局



    

    

2.清单文件给权限



    
    

    
        
            
                

                
            
        
    

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.运行

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/792127.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存