Back to project page Common-Library.
The source code is released under:
Apache License
If you think the Android project Common-Library listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.morgan.library.utils; /*from w w w .jav a 2 s .co m*/ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.media.AudioManager; import android.net.Uri; import android.os.Build; import android.telephony.SmsManager; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.view.KeyEvent; import android.widget.Toast; /** * ?????????????? * * @author Morgan.Ji * */ public class PhoneUtils { public static String TAG = PhoneUtils.class.getSimpleName(); /** * ????? * * @param context */ public static void endCall(Context context) { try { Object telephonyObject = getTelephonyObject(context); if (null != telephonyObject) { Class telephonyClass = telephonyObject.getClass(); Method endCallMethod = telephonyClass.getMethod("endCall"); endCallMethod.setAccessible(true); endCallMethod.invoke(telephonyObject); } } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } private static Object getTelephonyObject(Context context) { Object telephonyObject = null; try { // ????iTelephony TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); // Will be used to invoke hidden methods with reflection // Get the current object implementing ITelephony interface Class telManager = telephonyManager.getClass(); Method getITelephony = telManager .getDeclaredMethod("getITelephony"); getITelephony.setAccessible(true); telephonyObject = getITelephony.invoke(telephonyManager); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return telephonyObject; } /** * ???????????????????????????android 2.3?????????? * * @param context */ private static void answerRingingCallWithReflect(Context context) { try { Object telephonyObject = getTelephonyObject(context); if (null != telephonyObject) { Class telephonyClass = telephonyObject.getClass(); Method endCallMethod = telephonyClass .getMethod("answerRingingCall"); endCallMethod.setAccessible(true); endCallMethod.invoke(telephonyObject); // ITelephony iTelephony = (ITelephony) telephonyObject; // iTelephony.answerRingingCall(); } } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } /** * ????????????????????????????????????? * * @param context */ private static void answerRingingCallWithBroadcast(Context context) { AudioManager localAudioManager = (AudioManager) context .getSystemService(Context.AUDIO_SERVICE); // ????????????? boolean isWiredHeadsetOn = localAudioManager.isWiredHeadsetOn(); if (!isWiredHeadsetOn) { Intent headsetPluggedIntent = new Intent(Intent.ACTION_HEADSET_PLUG); headsetPluggedIntent.putExtra("state", 1); headsetPluggedIntent.putExtra("microphone", 0); headsetPluggedIntent.putExtra("name", ""); context.sendBroadcast(headsetPluggedIntent); 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); context.sendOrderedBroadcast(meidaButtonIntent, null); Intent headsetUnpluggedIntent = new Intent( Intent.ACTION_HEADSET_PLUG); headsetUnpluggedIntent.putExtra("state", 0); headsetUnpluggedIntent.putExtra("microphone", 0); headsetUnpluggedIntent.putExtra("name", ""); context.sendBroadcast(headsetUnpluggedIntent); } else { 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); context.sendOrderedBroadcast(meidaButtonIntent, null); } } /** * ??????? * * @param context */ public static void answerRingingCall(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { // 2.3?2.3???? answerRingingCallWithBroadcast(context); } else { answerRingingCallWithReflect(context); } } /** * ???? * * @param context * @param phoneNumber */ public static void callPhone(Context context, String phoneNumber) { if (!TextUtils.isEmpty(phoneNumber)) { try { Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber)); context.startActivity(callIntent); } catch (Exception e) { e.printStackTrace(); } } } /** * ???? * * @param context * @param phoneNumber */ public static void dialPhone(Context context, String phoneNumber) { if (!TextUtils.isEmpty(phoneNumber)) { try { Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)); context.startActivity(callIntent); } catch (Exception e) { e.printStackTrace(); } } } public static void editMessage(Context context, String phoneNumber, String msg) { Uri smsToUri = Uri.parse("smsto:" + phoneNumber); Intent msgIntent = new Intent(Intent.ACTION_SENDTO, smsToUri); msgIntent.putExtra("sms_body", msg); context.startActivity(msgIntent); } public static void editMessage(Context context, String msg) { Intent msgIntent = new Intent(); msgIntent.setAction(Intent.ACTION_SENDTO); msgIntent.putExtra("sms_body", msg); context.startActivity(msgIntent); } public static void sendMessage(Context context, String phoneNumber, String msg) { SmsManager smsManager = SmsManager.getDefault(); PendingIntent sentIntent = PendingIntent.getBroadcast(context, 0, new Intent(), 0); smsManager.sendTextMessage(phoneNumber, null, msg, sentIntent, null); context.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "???????????", Toast.LENGTH_SHORT).show(); } }, new IntentFilter("SENT_SMS_ACTION")); } }