网上扒的通过添加一个ITelephony.aidl来反射可是并没有成功。提示无法引入这个文件。
这里解释了一下原理;
使用java 反射来获取安卓内部的私有方法
TelephonyManager 类是由远程服务来实现的,它实质是
package org.nd.ui; import android.app.Fragment; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.CheckBox; import android.widget.TextView; import android.widget.Toast; import org.nd.R; import java.lang.reflect.Method; import org.nd.common.Common; /** * Created by HAO on 2015/7/17. */ public class FragmentCallPhone extends Fragment implements View.OnClickListener { public FragmentCallPhone() { } private Button btnCall, btnEnd; private TextView edit_phone; private static final int AUTO_END_CALL_AFTER_ONE_MINUTE = 10000; private Handler mHandler; private Handler cutHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case Common.HEART_BEAT: Log.d("org.nd", "HEART_BEAT_TWO"); break; case Common.REFLECTION_ERROR: Log.d("org.nd", "HEART_BEAT_THREE"); break; case Common.HEART_BEAT_TWO: Log.d("org.nd", "HEART_BEAT_ONE "); break; } } }; private boolean call_once = true; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_layout_call, container, false); btnCall = (Button) view.findViewById(R.id.call_phone); btnCall.setOnClickListener(this); btnEnd = (Button) view.findViewById(R.id.dial_and_call); btnEnd.setOnClickListener(this); edit_phone = (TextView) view.findViewById(R.id.edit_phone); MyPhoneListener phoneListener = new MyPhoneListener(); TelephonyManager telephonyManager = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); mHandler = new Handler(); return view; } @Override public void onResume() { super.onResume(); // btnCall.callOnClick(); } @Override public void onClick(View v) { String uri, number = edit_phone.getText().toString(); switch (v.getId()) { case R.id.call_phone: if (number.isEmpty()) number = "10000"; // if(!autoEnd.isChecked()) mHandler.postDelayed(new Runnable() { @Override public void run() { call_once = false; TelephonyManager telephonyManager = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE); Message msg = new Message(); try { Class c = Class.forName(telephonyManager.getClass().getName()); Method m = c.getDeclaredMethod("getITelephony"); m.setAccessible(true); Object telephonyService = m.invoke(telephonyManager); // Get the internal ITelephony object c = Class.forName(telephonyService.getClass().getName()); // Get its class m = c.getDeclaredMethod("endCall"); // Get the "endCall()" method m.setAccessible(true); // Make it accessible m.invoke(telephonyService); // invoke endCall() msg.what = Common.HEART_BEAT; cutHandler.sendMessage(msg); } catch (Exception e) { msg = new Message(); msg.what = Common.REFLECTION_ERROR; cutHandler.sendMessage(msg); Log.d("org.nd", e.toString()); } } }, AUTO_END_CALL_AFTER_ONE_MINUTE); uri = "tel:" + number; Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri)); startActivity(callIntent); Message msg = new Message(); msg.what = Common.HEART_BEAT_TWO; cutHandler.sendMessage(msg); break; case R.id.dial_and_call: uri = "tel:" + edit_phone.getText().toString(); Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse(uri)); startActivity(dialIntent); break; } } private class MyPhoneListener extends PhoneStateListener { private boolean onCall = false; @Override public void onCallStateChanged(int state, String incomingNumber) { switch (state) { case TelephonyManager.CALL_STATE_RINGING: // phone ringing... Toast.makeText(getActivity(), incomingNumber + " calls you", Toast.LENGTH_LONG).show(); break; case TelephonyManager.CALL_STATE_OFFHOOK: // one call exists that is dialing, active, or on hold Toast.makeText(getActivity(), "on call...", Toast.LENGTH_LONG).show(); //because user answers the incoming call onCall = true; break; case TelephonyManager.CALL_STATE_IDLE: // in initialization of the class and at the end of phone call // detect flag from CALL_STATE_OFFHOOK if (onCall == true) { Toast.makeText(getActivity(), "restart app after call", Toast.LENGTH_LONG).show(); // restart our application Intent restart = getActivity().getBaseContext().getPackageManager(). getLaunchIntentForPackage(getActivity().getBaseContext().getPackageName()); restart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(restart); onCall = false; } break; default: break; } } } }
调用远程电话服务,这个工作具体是由AIDL来做的,remote procedure call (RPC)
这样远程服务使用公有TelephonyManager 就可以不暴露实现,你需要做的就是利用 getITelephony() 来得到远程程序调用的客户端,此方法返回一个ITelephony类型的对象。
有了这个对象,就可以得到其类对象和内部方法endCall() ,这样我们就能调用这个方法。
现在这个endCall() 是运行在远程程序调用的客户端,它可以发送消息给 远程电话服务(运行在远程服务中),要求终止当前通话。
由于源代码 ITelephony.aidl 是公开的,你可以将代码放在你的工程中,IDE会自动生成ITelephony.java(自动包含了RPC的客户端)
当你在安卓设备上运行时,会调用安卓框架里的ITelephony 对象,并将其转换成 com.android.internal.telephony.ITelephony
————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
附上我的实现:
AndroidManifest.xml
... <uses-permission android:name="android.permission.CALL_PHONE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="ANDROID.PERMISSION.MODIFY_PHONE_STATE"/> ...
FragmentCallPhone.java
时间: 2024-10-17 02:47:13