Android挂断和接听电话
2013-08-24 18:07
225 查看
package com.gionee.call.test;
import java.lang.reflect.Method;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import com.android.internal.telephony.ITelephony;
public class PhoneUtils {
/**
* 根据传入的TelephonyManager来取得系统的ITelephony实例.
* @param telephony
* @return 系统的ITelephony实例
* @throws Exception
*/
private static String TAG="song";
public static ITelephony getITelephony(TelephonyManager telephony) throws Exception {
Method getITelephonyMethod = telephony.getClass().getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);//私有化函数也能使用
return (ITelephony)getITelephonyMethod.invoke(telephony);
}
//自动接听
public static void autoAnswerPhone(Context c,TelephonyManager tm) {
try {
Log.i(TAG, "autoAnswerPhone");
ITelephony itelephony = getITelephony(tm);
// itelephony.silenceRinger();
itelephony.answerRingingCall();
} catch (Exception e) {
e.printStackTrace();
try {
Log.e(TAG, "用于Android2.3及2.3以上的版本上");
Intent intent = new Intent("android.intent.action.MEDIA_BUTTON");
KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_HEADSETHOOK);
intent.putExtra("android.intent.extra.KEY_EVENT", keyEvent);
c.sendOrderedBroadcast(intent,
"android.permission.CALL_PRIVILEGED");
intent = new Intent("android.intent.action.MEDIA_BUTTON");
keyEvent = new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_HEADSETHOOK);
intent.putExtra("android.intent.extra.KEY_EVENT", keyEvent);
c.sendOrderedBroadcast(intent,
"android.permission.CALL_PRIVILEGED");
Intent localIntent1 = new Intent(Intent.ACTION_HEADSET_PLUG);
localIntent1.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
localIntent1.putExtra("state", 1);
localIntent1.putExtra("microphone", 1);
localIntent1.putExtra("name", "Headset");
c.sendOrderedBroadcast(localIntent1,
"android.permission.CALL_PRIVILEGED");
Intent localIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON);
KeyEvent localKeyEvent1 = new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_HEADSETHOOK);
localIntent2.putExtra("android.intent.extra.KEY_EVENT",
localKeyEvent1);
c.sendOrderedBroadcast(localIntent2,
"android.permission.CALL_PRIVILEGED");
Intent localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON);
KeyEvent localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_HEADSETHOOK);
localIntent3.putExtra("android.intent.extra.KEY_EVENT",
localKeyEvent2);
c.sendOrderedBroadcast(localIntent3,
"android.permission.CALL_PRIVILEGED");
Intent localIntent4 = new Intent(Intent.ACTION_HEADSET_PLUG);
localIntent4.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
localIntent4.putExtra("state", 0);
localIntent4.putExtra("microphone", 1);
localIntent4.putExtra("name", "Headset");
c.sendOrderedBroadcast(localIntent4,
"android.permission.CALL_PRIVILEGED");
} catch (Exception e2) {
e2.printStackTrace();
Intent meidaButtonIntent = new Intent(
Intent.ACTION_MEDIA_BUTTON);
KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_HEADSETHOOK);
meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
c.sendOrderedBroadcast(meidaButtonIntent, null);
}
}
}
//自动挂断
public static void endPhone(Context c,TelephonyManager tm) {
try {
Log.i(TAG, "endPhone");
ITelephony iTelephony;
Method getITelephonyMethod = TelephonyManager.class
.getDeclaredMethod("getITelephony", (Class[]) null);
getITelephonyMethod.setAccessible(true);
iTelephony = (ITelephony) getITelephonyMethod.invoke(tm,
(Object[]) null);
// 挂断电话
iTelephony.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
}
?
import java.lang.reflect.Method;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import com.android.internal.telephony.ITelephony;
public class PhoneUtils {
/**
* 根据传入的TelephonyManager来取得系统的ITelephony实例.
* @param telephony
* @return 系统的ITelephony实例
* @throws Exception
*/
private static String TAG="song";
public static ITelephony getITelephony(TelephonyManager telephony) throws Exception {
Method getITelephonyMethod = telephony.getClass().getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);//私有化函数也能使用
return (ITelephony)getITelephonyMethod.invoke(telephony);
}
//自动接听
public static void autoAnswerPhone(Context c,TelephonyManager tm) {
try {
Log.i(TAG, "autoAnswerPhone");
ITelephony itelephony = getITelephony(tm);
// itelephony.silenceRinger();
itelephony.answerRingingCall();
} catch (Exception e) {
e.printStackTrace();
try {
Log.e(TAG, "用于Android2.3及2.3以上的版本上");
Intent intent = new Intent("android.intent.action.MEDIA_BUTTON");
KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_HEADSETHOOK);
intent.putExtra("android.intent.extra.KEY_EVENT", keyEvent);
c.sendOrderedBroadcast(intent,
"android.permission.CALL_PRIVILEGED");
intent = new Intent("android.intent.action.MEDIA_BUTTON");
keyEvent = new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_HEADSETHOOK);
intent.putExtra("android.intent.extra.KEY_EVENT", keyEvent);
c.sendOrderedBroadcast(intent,
"android.permission.CALL_PRIVILEGED");
Intent localIntent1 = new Intent(Intent.ACTION_HEADSET_PLUG);
localIntent1.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
localIntent1.putExtra("state", 1);
localIntent1.putExtra("microphone", 1);
localIntent1.putExtra("name", "Headset");
c.sendOrderedBroadcast(localIntent1,
"android.permission.CALL_PRIVILEGED");
Intent localIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON);
KeyEvent localKeyEvent1 = new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_HEADSETHOOK);
localIntent2.putExtra("android.intent.extra.KEY_EVENT",
localKeyEvent1);
c.sendOrderedBroadcast(localIntent2,
"android.permission.CALL_PRIVILEGED");
Intent localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON);
KeyEvent localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_HEADSETHOOK);
localIntent3.putExtra("android.intent.extra.KEY_EVENT",
localKeyEvent2);
c.sendOrderedBroadcast(localIntent3,
"android.permission.CALL_PRIVILEGED");
Intent localIntent4 = new Intent(Intent.ACTION_HEADSET_PLUG);
localIntent4.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
localIntent4.putExtra("state", 0);
localIntent4.putExtra("microphone", 1);
localIntent4.putExtra("name", "Headset");
c.sendOrderedBroadcast(localIntent4,
"android.permission.CALL_PRIVILEGED");
} catch (Exception e2) {
e2.printStackTrace();
Intent meidaButtonIntent = new Intent(
Intent.ACTION_MEDIA_BUTTON);
KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_HEADSETHOOK);
meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
c.sendOrderedBroadcast(meidaButtonIntent, null);
}
}
}
//自动挂断
public static void endPhone(Context c,TelephonyManager tm) {
try {
Log.i(TAG, "endPhone");
ITelephony iTelephony;
Method getITelephonyMethod = TelephonyManager.class
.getDeclaredMethod("getITelephony", (Class[]) null);
getITelephonyMethod.setAccessible(true);
iTelephony = (ITelephony) getITelephonyMethod.invoke(tm,
(Object[]) null);
// 挂断电话
iTelephony.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
}
?
相关文章推荐
- Android拨打、接听、挂断电话操作
- Android 电话的反射调用机制实现静默接听电话
- Android开发之实现自动接听电话(绝…
- Android自动接听和挂断电话实现原理
- Android自动接听&挂断电话(包含怎么应对4.1以上版本的权限检)
- android 2.1 监听电话状态并自动接听来电
- Android自动接听&挂断电话(包含怎么应对4.1以上版本的权限检
- [Android]挂断、接听电话
- Android自动接听和挂断电话实现原理
- Android 电话的反射调用机制实现自动接听电话
- android 2.1 监听电话状态并自动接听来电
- android电话自动接听/挂断
- Android:电话接听
- Android 如何判断拨打电话对方是否接听
- Android 电话的反射调用机制实现静默接听电话
- [Android]挂断、接听电话
- [Android]Android5.0实现静默接听电话功能
- android实现 5.0 6.0手机自动接听电话功能
- [Android]挂断、接听电话
- android 2.1 监听电话状态并自动接听来电