页面布局文件代码  (  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. Oracle学习笔记(一)——并发与锁

    1 并发 多用户数据库管理系统的一个主要任务是对 并发(concurrency)进行控制,即对多个用户同时访问同一数据进行控制.当缺乏有效的并发控制时,修改数据的操作就不能保证正常,从而危害数据完整性 ...

  2. Codeforces 918C - The Monster

    918C - The Monster 思路1: 右键在新窗口打开图片 代码: #include<bits/stdc++.h> using namespace std; #define ll ...

  3. Codeforces 580A - Kefa and First Steps

    580A - Kefa and First Steps 思路:dp dp[i]表示包括前i个元素中a[i]在内的最大增序列. 代码: #include<bits/stdc++.h> usi ...

  4. API网关 动态路由、监控、授权、安全、调度

    1.API网关介绍 API网关是一个服务器,是系统的唯一入口.从面向对象设计的角度看,它与外观模式类似.API网关封装了系统内部架构,为每个客户端提供一个定制的API.它可能还具有其它职责,如身份验证 ...

  5. Linux 物理内存 buffer cache

    Linux下如何查内存信息,如内存总量.已使用量.可使用量.经常使用Windows操作系统的朋友,已经习惯了如果空闲的内存较多,心里比较踏实.当使用Linux时,可能觉的Linux物理内存很快被用光( ...

  6. 3-22 Ruby 编码规则(个人整理)

    编码规则 https://github.com/thoughtbot/guides/tree/master/style/ruby *Use a trailing comma after each it ...

  7. 想3分钟搭建图像识别系统?这里有一份TensorFlow速成教程(转)

    http://www.voidcn.com/article/p-wyaahqji-dr.html 从我们见到的各种图像识别软件来看,机器似乎能认出人脸.猫.狗.花草.各种汽车等等日常生活中出现的物体, ...

  8. 『Numpy』常用方法记录

    numpy教程 防止输出省略号 import numpy as np np.set_printoptions(threshold=np.inf) 广播机制 numpy计算函数返回默认是一维行向量: i ...

  9. python-day33--进程间通信(IPC)

    方式:队列(推荐使用) 一.基本情况 1.可以往队列里放任意类型的数据 2. 队列:先进先出 3. q=Queue(3) #可以设置队列中最多可以进入多少个值,也可以不设置 q.put('first' ...

  10. ES profile 性能优化用——返回各个shard的耗时

    Profile API 都说要致富先修路,要调优当然需要先监控啦,elasticsearch在很多层面都提供了stats方便你来监控调优,但是还不够,其实很多情况下查询速度慢很大一部分原因是糟糕的查询 ...