[Android]挂断、接听电话

一个很简陋的小例子

参考自:通过AIDL及反射机制,使用隐藏API挂断电话

 

个人理解上其实是同名类跨进程欺骗Dalvik VM,大伙儿可进一步联想扩展下功能,定会有惊喜!!!

以下为源码,仅做个人备份及参考。

 

view plain
  1. package lab.sodino.phonecall;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. public class PhoneCall extends Activity {  
  5.     /** Called when the activity is first created. */  
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.main);  
  10.     }  
  11. }  

 

view plain
  1. package lab.sodino.phonecall;  
  2. import android.app.Service;  
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.telephony.TelephonyManager;  
  7. import android.widget.Toast;  
  8. /** 
  9.  *@author Sodino Email:sodinoopen@hotmail<br/> 
  10.  *@version 2011-2-6 下午10:38:55 
  11.  */  
  12. public class PhoneListener extends BroadcastReceiver {  
  13.     public void onReceive(Context context, Intent intent) {  
  14.         String action = intent.getAction();  
  15.         LogOut.out(this"ord:" + isOrderedBroadcast() + " act:" + action);  
  16.         Toast toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);  
  17.         if (action.equals("android.intent.action.NEW_OUTGOING_CALL")) {  
  18.             toast.setText("Outgoing");  
  19.         } else {  
  20.             toast.setText("InComing");  
  21.             TelephonyManager tm = (TelephonyManager) context  
  22.                     .getSystemService(Service.TELEPHONY_SERVICE);  
  23.             boolean incomingFlag = false;  
  24.             String incoming_number = "";  
  25.             switch (tm.getCallState()) {  
  26.             case TelephonyManager.CALL_STATE_RINGING:  
  27.                 incomingFlag = true;// 标识当前是来电  
  28.                 incoming_number = intent.getStringExtra("incoming_number");  
  29.                 LogOut.out(this"RINGING :" + incoming_number);  
  30.                 try {  
  31.                     Thread.sleep(3000);  
  32.                 } catch (InterruptedException e) {  
  33.                     e.printStackTrace();  
  34.                 }  
  35.                 Intent tmpI = new Intent(context, ShowAct.class);  
  36.                 tmpI.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  37.                 context.startActivity(tmpI);  
  38.                 break;  
  39.             case TelephonyManager.CALL_STATE_OFFHOOK:  
  40.                 if (incomingFlag) {  
  41.                     LogOut.out(this"incoming ACCEPT :" + incoming_number);  
  42.                 }  
  43.                 break;  
  44.             case TelephonyManager.CALL_STATE_IDLE:  
  45.                 if (incomingFlag) {  
  46.                     LogOut.out(this"incoming IDLE");  
  47.                 }  
  48.                 break;  
  49.             }  
  50.         }  
  51.         toast.show();  
  52.     }  
  53. }  

 

view plain
  1. package lab.sodino.phonecall;  
  2. import java.lang.reflect.Method;  
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.telephony.TelephonyManager;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import com.android.internal.telephony.ITelephony;  
  10. /** 
  11.  *@author Sodino Email:sodinoopen@hotmail<br/> 
  12.  *@version 2011-2-6 下午08:33:25 
  13.  */  
  14. public class ShowAct extends Activity {  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         getWindow().setWindowAnimations(0);  
  18.         setContentView(R.layout.show);  
  19.         Button btnRefuse = (Button) findViewById(R.id.btnRefuse);  
  20.         btnRefuse.setOnClickListener(new Button.OnClickListener() {  
  21.             public void onClick(View view) {  
  22.                 endCall();  
  23.             }  
  24.         });  
  25.         Button btnReceiver = (Button) findViewById(R.id.btnRefuse);  
  26.         btnReceiver.setOnClickListener(new Button.OnClickListener() {  
  27.             public void onClick(View view) {  
  28.                 answerCall();  
  29.             }  
  30.         });  
  31.     }  
  32.     private void answerCall() {  
  33.         TelephonyManager telMag = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
  34.         Class<TelephonyManager> c = TelephonyManager.class;  
  35.         Method mthEndCall = null;  
  36.         try {  
  37.             mthEndCall = c.getDeclaredMethod("getITelephony", (Class[]) null);  
  38.             mthEndCall.setAccessible(true);  
  39.             ITelephony iTel = (ITelephony) mthEndCall.invoke(telMag,  
  40.                     (Object[]) null);  
  41.             iTel.answerRingingCall();  
  42.             LogOut.out(this, iTel.toString());  
  43.         } catch (Exception e) {  
  44.             e.printStackTrace();  
  45.         }  
  46.         LogOut.out(this"answer call");  
  47.     }  
  48.     private void endCall() {  
  49.         TelephonyManager telMag = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
  50.         Class<TelephonyManager> c = TelephonyManager.class;  
  51.         Method mthEndCall = null;  
  52.         try {  
  53.             mthEndCall = c.getDeclaredMethod("getITelephony", (Class[]) null);  
  54.             mthEndCall.setAccessible(true);  
  55.             ITelephony iTel = (ITelephony) mthEndCall.invoke(telMag,  
  56.                     (Object[]) null);  
  57.             iTel.endCall();  
  58.             LogOut.out(this, iTel.toString());  
  59.         } catch (Exception e) {  
  60.             e.printStackTrace();  
  61.         }  
  62.         LogOut.out(this"endCall test");  
  63.     }  
  64.     public void onStart() {  
  65.         super.onStart();  
  66.         // 1.经测试,貌似无法杀掉phone程序  
  67.         // ActivityManager actMag = (ActivityManager)  
  68.         // getSystemService(Context.ACTIVITY_SERVICE);  
  69.         // actMag.killBackgroundProcesses("com.android.phone");  
  70.         // 2.来个恶搞:直接回桌面去,heihei...  
  71.         // Intent tmpI = new Intent(Intent.ACTION_MAIN);  
  72.         // tmpI.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  73.         // tmpI.addCategory(Intent.CATEGORY_HOME);  
  74.         // startActivity(tmpI);  
  75.         // LogOut.out(this, "killBgPro&&startHome");  
  76.     }  
  77.     public void onPause() {  
  78.         super.onPause();  
  79.         finish();  
  80.     }  
  81. }  

 

关键文件:ITelephony.aidl

view plain
  1. package com.android.internal.telephony;  
  2. interface ITelephony{  
  3.     boolean endCall();  
  4.     void answerRingingCall();  
  5. }  

 

AndroidManifest.xml

view plain
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="lab.sodino.phonecall" android:versionCode="1"  
  4.     android:versionName="1.0">  
  5.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  6.         <activity android:name=".PhoneCall" android:label="@string/app_name">  
  7.             <intent-filter>  
  8.                 <action android:name="android.intent.action.MAIN" />  
  9.                 <category android:name="android.intent.category.LAUNCHER" />  
  10.             </intent-filter>  
  11.         </activity>  
  12.         <activity android:name=".ShowAct"  
  13.             android:launchMode="singleInstance">  
  14.         </activity>  
  15.         <receiver android:name=".PhoneListener">  
  16.             <intent-filter android:priority="-1000">  
  17.                 <action android:name="android.intent.action.PHONE_STATE"></action>  
  18.                 <category android:name="android.intent.category.DEFAULT"></category>  
  19.             </intent-filter>  
  20.             <intent-filter>  
  21.                 <action android:name="android.intent.action.NEW_OUTGOING_CALL"></action>  
  22.                 <category android:name="android.intent.category.DEFAULT"></category>  
  23.             </intent-filter>  
  24.         </receiver>  
  25.     </application>  
  26.     <uses-sdk android:minSdkVersion="3" />  
  27.     <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"></uses-permission>  
  28.     <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>  
  29.     <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>  
  30. </manifest>   

你可能感兴趣的:(android,object,service,null,button,encoding)