页面布局文件代码  (  res下面的layout下面的activity_main.xml代码 )

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.u.phone.MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="28dp"
android:text="手机号码:" />

<EditText
android:id="@+id/phonetext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="90dp"
android:layout_marginTop="16dp"
android:text=""/>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="70dp"
android:text="呼叫几秒:" />

<EditText
android:id="@+id/shijiantext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="90dp"
android:layout_marginTop="60dp"
android:text="5"/>

<Button
android:id="@+id/submitbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginLeft="30dp"
android:layout_marginTop="53dp"
android:text="拨打电话" />

<Button
android:id="@+id/clearcall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginLeft="160dp"
android:layout_marginTop="53dp"
android:text="停止呼叫" />

</RelativeLayout>

MainActivity.java代码  ( src下面的com.u.phone 下面的 MainActivity.java )

package com.u.phone;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.content.Intent;
import android.net.Uri;
import com.android.internal.telephony.ITelephony;
import android.telephony.TelephonyManager;
import android.content.Context;
import java.lang.reflect.Method;
import android.view.View.OnClickListener;

public class MainActivity extends ActionBarActivity {
private Button start=null;
private Button stop =null;
private EditText photoNo=null;
private boolean runnable=true;
private boolean endCall=false;
private String telePhotoNo=null;
private EditText sjText=null;
private String sjNum=null;
ITelephony iPhoney=null;
//private TelephonyManager;
Thread t=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//取得资源
start=(Button)findViewById(R.id.submitbutton);
stop=(Button)findViewById(R.id.clearcall);
photoNo=(EditText)findViewById(R.id.phonetext);
sjText=(EditText)findViewById(R.id.shijiantext);
final TelephonyManager tm=(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
iPhoney=getITelephony(this);//获取电话实例
//增加事件响应
start.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
telePhotoNo=photoNo.getText().toString().trim();
sjNum=sjText.getText().toString().trim();
//System.out.println(telePhotoNo);
t.start();
}
});
//增加事件响应
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
runnable=false;
System.exit(0);
// finish();
}
});
//线程
t=new Thread(new Runnable() {

@Override
public void run() {
try {
while(runnable){

Thread.sleep(2000);//延时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(Integer.parseInt(sjNum)*1000);//延时10s
endCall= iPhoney.endCall();
//System.out.println("是否成功挂断:"+endCall);
}

}
} catch (Exception e)
{
e.printStackTrace();
}
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* 通过反射得到实例
* @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;
}

}

电话权限: AndroidManifest.xml里加入下面2行权限代码

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

android 自动拨打电话 挂断电话代码的更多相关文章

  1. Android之——自己主动挂断电话的实现

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47072451 通过<Android之--AIDL小结>与<And ...

  2. Android 实现自动接听和挂断电话功能

    添加权限 <uses-permission android:name="android.permission.CALL_PHONE"/> <uses-permis ...

  3. Android自动接听&挂断电话(包含怎么应对4.1以上版本的权限检

    一  前言 这两天要研究类似白名单黑名单以及手势自动接听的一些功能,所以呢,自然而然的涉及到怎么自动接听/挂断电话的功能了.对于自动接听这一块,android4.1版本及其以上的版本和之前的版本处理逻 ...

  4. Android手机拨打电话的开发实例

    一部手机最常用的功能就是打电话和发短信了,在Android开发中我们如何通过程序拨打电话呢?本文就给出一个用Android手机拨打电话的简单的实例. 下面是开发此实例的具体步骤: 一.新建一个Andr ...

  5. Android 直接拨打电话界面

    Android 拨号界面和直接拨打电话界面代码控制 //定义TAG为空 private static final String TAG = null; //定义Button的点击事件 tell.set ...

  6. 脚本控制向Android模拟拨打电话,发送短信,定位设置功能

    做行为触发的时候要向模拟器实现拨打电话,发送短信,定位设置的的功能,可以很方便通过telnet localhost  5554实现. 写个脚本很快的搞定了.网上资料很多,脚本的很少,也所积点德啦. 写 ...

  7. Android MonkeyRunner自动拨打电话

    from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice import time device = MonkeyRunner.wa ...

  8. Android 学习第11课,android 实现拨打电话的功能

    1. 先布局界面,界面采用线性垂直方式来布局 在layout 界面文件中 activity_main.xml 中 <LinearLayout xmlns:android="http:/ ...

  9. Android通过AIDL和反射调用系统拨打电话和挂断电话

    首先在项目中添加ITelephony.aidl文件,我的如下: /* * Copyright (C) 2007 The Android Open Source Project * * Licensed ...

随机推荐

  1. Codeforces 385C - Bear and Prime Numbers(素数筛+前缀和+hashing)

    385C - Bear and Prime Numbers 思路:记录数组中1-1e7中每个数出现的次数,然后用素数筛看哪些能被素数整除,并加到记录该素数的数组中,然后1-1e7求一遍前缀和. 代码: ...

  2. oralce表空间使用情况查询

    SELECT UPPER(F.TABLESPACE_NAME) TABLESPACE_NAME, -- 表空间名, D.TOT_GROOTTE_MB TOT_GROOTTE_MB, -- 表空间大小( ...

  3. 雷林鹏分享:C# 事件(Event)

    C# 事件(Event) 事件(Event) 基本上说是一个用户操作,如按键.点击.鼠标移动等等,或者是一些出现,如系统生成的通知.应用程序需要在事件发生时响应事件.例如,中断.事件是用于进程间通信. ...

  4. LeetCode--171--Excel表列序号

    问题描述: 给定一个Excel表格中的列名称,返回其相应的列序号. 例如, A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -& ...

  5. 1月11日Atom 插件安装。

    查看已安装的Atom插件(前提:已经安装Atom) 打开终端 输入apm ls命令,回车. 未安装任何插件时,显示如下 Built-in Atom packages (89) ...此处省略... / ...

  6. 在Windows下配置svn服务端钩子程序(部分)

    需求一,svn提交时必须填写log日志的需求,如何进行配置呢?请看下面. 需要在版本库目录下找到hooks文件夹,我的版本库是dxoffice,所以是这个目录,你要找自己的目录 然后进入,创建一个pr ...

  7. oracle12c新特点之可插拔数据库(Pluggable Database,PDB)

    1.    12c PDB新特点的优势 1)    可以把多个PDB集成进一个平台. 2)    可以快速提供一个新的PDB或一个已有PDB的克隆. 3)    通过拔插技术,可以快速把存在的数据库重 ...

  8. spring cloud学习(六)Spring Cloud Config

    Spring Cloud Config 参考个人项目 参考个人项目 : (希望大家能给个star~) https://github.com/FunriLy/springcloud-study/tree ...

  9. win10解除密码

  10. sqlite 查询数据库中所有的表名,判断某表是否存在,将某列所有数值去重后获得数量

    1.列出当前db文件中所有的表的表名 SQL语句:SELECT * FROM sqlite_master WHERE type='table'; 结构如下: 注:网上有人说可以带上db文件的名称,如: ...