实现电话自动拨打、挂断

声明:该软件只是用以学习android系统的相关知识,任何参考该博客文章的其他行为均与该博客文章的作者无关。

软件要实现的大致功能是:通过输入框获取需要拨打的电话号码,电泳android打电话功能进行拨号,判断电话是否打通,如果打通则自动挂断。

1.实现自动拨打功能:调用android自带intent传入Uri,代码如下: Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+telePhotoNo));
     startActivity(intent); 运行该代码会转到电话拨打页面,其中telePhotoNo为你所有要拨打的电话号码。

2.自动挂断功能:由于android系统将ITelephony对象私用化,顾无法调用该对象的endCall方法,只用通过AIDL和Java反射机制获取对象并调用endCall方法。步骤如下:

(1)子src文件下新建一个com.android.internal.telephony包,并添加adil文件,文件内容如下:

                                                     package com.android.internal.telephony;
                                                         interface ITelephony{
                                                       boolean endCall();
                                                        void answerRingingCall();
保存该aidl文件后android ADT会自动生成ITelephony.java类。

(2)通过发射机制获取ITelephony对象。代码如下:

/**
     * 通过反射得到实例
     * @param context
     * @return
     */
    private static ITelephony getITelephony(Context context) {
        TelephonyManager mTelephonyManager = (TelephonyManager) context
                .getSystemService(TELEPHONY_SERVICE);
        Class<TelephonyManager> c = TelephonyManager.class;
        Method getITelephonyMethod = null;
        try {
            getITelephonyMethod = c.getDeclaredMethod("getITelephony",
                    (Class[]) null); // 获取声明的方法
            getITelephonyMethod.setAccessible(true);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        ITelephony iTelephony=null;
        try {
             iTelephony = (ITelephony) getITelephonyMethod.invoke(
                    mTelephonyManager, (Object[]) null); // 获取实例
            return iTelephony;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return iTelephony;
    }
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

项目源码如下:

1.layout/main.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <EditText
        android:id="@+id/telephotono"
        android:layout_width="fill_parent"
        android:layout_height="60dip"
        android:text="请输入电话号码"
        android:layout_marginTop="10dip"
        android:layout_marginLeft="0dip"
        />
    <Button
        android:id="@+id/start"
        android:layout_width="fill_parent"
        android:layout_height="60dip"
        android:text="开始"
        android:layout_marginTop="10dip"
        android:layout_marginLeft="0dip"
        />
 <Button
        android:id="@+id/stop"
        android:layout_width="fill_parent"
        android:layout_height="60dip"
        android:text="停止"
        android:layout_marginTop="10dip"
        android:layout_marginLeft="0dip"
        />
   
</LinearLayout>

2.TeleCastActivity.java 代码如下:

package com.cbq.tel;

import java.lang.reflect.Method;

import com.android.internal.telephony.ITelephony;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class TeleCastActivity extends Activity {
    /** Called when the activity is first created. */
 private Button start=null;
 private Button stop =null;
 private EditText photoNo=null;
 private boolean runnable=true;
 private boolean endCall=false;
    private String telePhotoNo=null;
  ITelephony iPhoney=null;
 //private TelephonyManager;
 Thread t=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        start=(Button)findViewById(R.id.start);
        stop=(Button)findViewById(R.id.stop);
        photoNo=(EditText)findViewById(R.id.telephotono);
       
        final TelephonyManager tm=(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
        iPhoney=getITelephony(this);//获取电话实例
        start.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    if((photoNo.getText().toString()!=null && photoNo.getText().toString().length()>0) && !photoNo.getText().toString().equals("请输入电话号码")){
        telePhotoNo=photoNo.getText().toString().trim();
        //System.out.println(telePhotoNo);
     t.start();
    }
    else
     Toast.makeText(TeleCastActivity.this, "请输入你所要拨打的电话号码", 2000).show();
    
   }
  });
        stop.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    runnable=false;
    System.exit(0);
   // finish();
    
   }
  });
       
        photoNo.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    if(photoNo.getText().toString().equals("请输入电话号码"))
    photoNo.setText("");//
    
   }
  });
        
         t=new Thread(new Runnable() {
   
   @Override
   public void run() {
   try {
      while(runnable){
      
    Thread.sleep(5000);//延时5s
    int state=tm.getCallState();
    if(state==TelephonyManager.CALL_STATE_IDLE){
     Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+telePhotoNo));
     startActivity(intent);
    }
    if(state==TelephonyManager.CALL_STATE_OFFHOOK){
     Thread.sleep(10000);
     endCall= iPhoney.endCall();
     //System.out.println("是否成功挂断:"+endCall);
    }
    
     
      }
   } catch (Exception e)
      {
    e.printStackTrace();
      }
   }
  });
    }
   
    /**
     * 通过反射得到实例
     * @param context
     * @return
     */
    private static ITelephony getITelephony(Context context) {
        TelephonyManager mTelephonyManager = (TelephonyManager) context
                .getSystemService(TELEPHONY_SERVICE);
        Class<TelephonyManager> c = TelephonyManager.class;
        Method getITelephonyMethod = null;
        try {
            getITelephonyMethod = c.getDeclaredMethod("getITelephony",
                    (Class[]) null); // 获取声明的方法
            getITelephonyMethod.setAccessible(true);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        ITelephony iTelephony=null;
        try {
             iTelephony = (ITelephony) getITelephonyMethod.invoke(
                    mTelephonyManager, (Object[]) null); // 获取实例
            return iTelephony;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return iTelephony;
    }

  
}

注意:应用能够调用拨打电话是需要添加相关的权限:

 拨打权限:
<uses-permission android:name="android.permission.CALL_PHONE"/>   

查看电话状态权限:

  <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

 

 

软件运行的效果图:

                                                                                                              

 

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
编写一个完整的软电话功能可能会涉及到很多细节和复杂的逻辑。下面是一个简单的示例代码,演示了如何在Golang中创建一个基本的软电话,包括注册、挂断和接听功能: ```go package main import ( "bufio" "fmt" "log" "os" "os/signal" "strings" "sync" "time" ) // Softphone 表示软电话结构 type Softphone struct { registered bool inCall bool mutex sync.Mutex } // Register 方法用于注册软电话 func (s *Softphone) Register() { if s.registered { fmt.Println("Softphone is already registered.") return } fmt.Println("Registering the softphone...") time.Sleep(2 * time.Second) // 模拟注册过程 s.mutex.Lock() defer s.mutex.Unlock() s.registered = true fmt.Println("Softphone registered successfully.") } // Unregister 方法用于注销软电话 func (s *Softphone) Unregister() { if !s.registered { fmt.Println("Softphone is not registered.") return } fmt.Println("Unregistering the softphone...") time.Sleep(2 * time.Second) // 模拟注销过程 s.mutex.Lock() defer s.mutex.Unlock() s.registered = false fmt.Println("Softphone unregistered successfully.") } // Call 方法用于拨打电话 func (s *Softphone) Call() { if !s.registered { fmt.Println("Softphone is not registered. Cannot make a call.") return } if s.inCall { fmt.Println("Already in a call. Cannot make a new call.") return } fmt.Println("Calling...") time.Sleep(2 * time.Second) // 模拟呼叫过程 s.mutex.Lock() defer s.mutex.Unlock() s.inCall = true fmt.Println("Call connected.") } // Hangup 方法用于挂断电话 func (s *Softphone) Hangup() { if !s.inCall { fmt.Println("Not in a call. Cannot hang up.") return } fmt.Println("Hanging up...") time.Sleep(2 * time.Second) // 模拟挂断过程 s.mutex.Lock() defer s.mutex.Unlock() s.inCall = false fmt.Println("Call disconnected.") } func main() { softphone := &Softphone{} // 处理Ctrl+C信号 sig := make(chan os.Signal, 1) signal.Notify(sig, os.Interrupt, os.Kill) go func() { <-sig softphone.Hangup() // 在收到信号时挂断电话 os.Exit(0) }() reader := bufio.NewReader(os.Stdin) fmt.Println("Welcome to the softphone!") fmt.Println("Commands: register, unregister, call, hangup, exit") for { fmt.Print("Enter a command: ") command, _ := reader.ReadString('\n') command = strings.TrimSuffix(command, "\n") switch command { case "register": softphone.Register() case "unregister": softphone.Unregister() case "call": softphone.Call() case "hangup": softphone.Hangup() case "exit": os.Exit(0) default: fmt.Println("Invalid command. Please try again.") } } } ``` 这段代码创建了一个`Softphone`结构体,包含了软电话的注册状态和通话状态。`Register`方法用于注册软电话,`Unregister`方法用于注销软电话,`Call`方法用于拨打电话,`Hangup`方法用于挂断电话。 在`main`函数中,我们使用一个无限循环来接受用户的命令,并根据不同的命令调用相应的方法。通过处理Ctrl+C信号来优雅地退出软电话,并在收到信号时自动挂断电话。 请注意,这只是一个简单的示例,实际的软电话实现可能需要更多的功能和逻辑。希望对你有帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值