页面布局文件代码  (  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. 【Jmeter】Linux(Mac)上使用最新版本Jmeter(5.0)做性能测试

    本文我们一起来学习在Linux(Mac)上利用Jmeter进行性能测试并生成测试报告的方法. 环境准备 JDK 访问这个地址 [JDK11.01],根据实际环境下载一个JDK. Jmeter Jmet ...

  2. Linux中CentOS6.5 64位 系统下安装docker步骤

    CentOS6.5 64位    (docker目前仅支持64位)内核必须在3.10及以上 1. uname -r  查看内核版本 2. 升级内核到3.10版本(带aufs模块)    cd /etc ...

  3. Codeforces 861D - Polycarp's phone book

    861D - Polycarp's phone book 思路:用map做的话,只能出现一次循环,否则会超时. 代码: #include<bits/stdc++.h> using name ...

  4. Codeforces A - Bear and Prime 100(交互题)

    A - Bear and Prime 100 思路:任何一个合数都可以写成2个以上质数的乘积.在2-100中,除了4,9,25,49外都可以写成两个以上不同质数的乘积. 所以打一个质数加这四个数的表: ...

  5. Hibernate实例二

    Hibernate实例二 一.测试openSession方法和getCurrentSession方法 hebernate中可以通过上述两种方法获取session对象以对数据库进行操作,下面的代码以及注 ...

  6. 【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(五)

    2.Find示例代码 (1)xaml文件: //添加Symbol命名空间 xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbol ...

  7. English trip -- VC(情景课)8 C

    MP review: 音标(Phonetic symbol) [ɪ]    lit adj. 照亮的,点着的(light的过去式及过去分词)n. (Lit)人名:(瑞典)利特:(老)李 [ʊ]   g ...

  8. Nginx+uWsgi生产部署Django

    部署得过程很简单,部署得核心在于,为什么要这样做,每一个参数代表什么意思,最终的目的是得了解,一些基概念性的东西. uWsgi简介 说Uwsgi之前,先说一下Wsgi. 什么是Wsgi? WSGI: ...

  9. (转)(HR面试)最常见的面试问题和技巧性答复

    (HR面试)最常见的面试问题和技巧性答复 1.请你自我介绍一下你自己? 回答提示:一般人回答这个问题过于平常,只说姓名.年龄.爱好.工作经验,这些在简历上都有.其实,企业最希望知道的是求职者能否胜任工 ...

  10. Python并行(parallel)之谈

    简介 可以先看看并发Concurrent与并行Parallel的区别 在谈并行前,头脑中总会浮出多线程.多进程.线程/进程同步.线程/进程通信等词语. 那为什么需要同步.通信,它们之间的作用是怎样的呢 ...