失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 实现电话自动拨打 挂断

实现电话自动拨打 挂断

时间:2022-08-16 12:24:52

相关推荐

实现电话自动拨打 挂断

声明:该软件只是用以学习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="/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 .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"/>

软件运行的效果图:

如果觉得《实现电话自动拨打 挂断》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。