1、首先在manifest中加入如下的权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE" >
</uses-permission>
红色的部分如果报错,则clean一下就好了。
<activity
android:name="com.example.tel.phonecall.PhoneCall"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.tel.phonecall.ShowAct"
android:launchMode="singleInstance" >
</activity>
<receiver
android:name="com.example.tel.phonecall.PhoneListener"
android:permission="android.permission.PROCESS_OUTGOING_CALLS" >
<intent-filter android:priority="-1000" >
<action android:name="android.intent.action.PHONE_STATE" >
</action>
<category android:name="android.intent.category.DEFAULT" >
</category>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" >
</action>
<category android:name="android.intent.category.DEFAULT" >
</category>
</intent-filter>
</receiver>
2、代码如下:
package com.example.tel.phonecall;}
package com.example.tel.phonecall;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class PhoneListener extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// LogOut.out(this, "ord:" + isOrderedBroadcast() + " act:" + action);
Log.i(this.getClass().getName(), "ord:" + isOrderedBroadcast()
+ " act:" + action);
Toast toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
if (action.equals("android.intent.action.NEW_OUTGOING_CALL")) {
toast.setText("Outgoing");
} else {
toast.setText("InComing");
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Service.TELEPHONY_SERVICE);
boolean incomingFlag = false;
String incoming_number = "";
switch (tm.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING:
incomingFlag = true;// 标识当前是来电
incoming_number = intent.getStringExtra("incoming_number");
Log.i(this.getClass().getName(), "RINGING :" + incoming_number);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent tmpI = new Intent(context, ShowAct.class);
tmpI.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(tmpI);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (incomingFlag) {
Log.i(this.getClass().getName(), "incoming ACCEPT :"
+ incoming_number);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (incomingFlag) {
Log.i(this.getClass().getName(), "incoming IDLE");
}
break;
}
}
toast.show();
}
}
package com.example.tel.phonecall;
import java.lang.reflect.Method;
import com.example.tel.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import com.android.internal.telephony.ITelephony;
public class ShowAct extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setWindowAnimations(0);
setContentView(R.layout.show);
Button btnRefuse = (Button) findViewById(R.id.btnEndcall);
btnRefuse.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
endCall();
}
});
Button btnReceiver = (Button) findViewById(R.id.btnAnswer);
btnReceiver.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
// answerCall();
// answerRingingCall(getApplication());
answerRingingCallWithBroadcast(getApplication());
}
});
}
private void endCall() {
TelephonyManager telMag = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Class<TelephonyManager> c = TelephonyManager.class;
Method mthEndCall = null;
try {
mthEndCall = c.getDeclaredMethod("getITelephony", (Class[]) null);
mthEndCall.setAccessible(true);
ITelephony iTel = (ITelephony) mthEndCall.invoke(telMag,
(Object[]) null);
iTel.endCall();
Log.i(this.getClass().getName(), iTel.toString());
} catch (Exception e) {
e.printStackTrace();
}
Log.i(this.getClass().getName(), "endCall test");
}
private void answerRingingCallWithBroadcast(Context context) {
AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
// 判断是否插上了耳机
if (!audioManager.isWiredHeadsetOn()) {
// 4.1以上系统限制了部分权限, 使用三星4.1版本测试提示警告:Permission Denial: not allowed to
// send broadcast android.intent.action.HEADSET_PLUG from pid=1324,
// uid=10017
// 这里需要注意一点,发送广播时加了权限“android.permission.CALL_PRIVLEGED”,
//则接受该广播时也需要增加该权限。但是4.1以上版本貌似这个权限只能系统应用才可以得到。测试的时候,
//自定义的接收器无法接受到此广播,后来去掉了这个权限,设为NULL便可以监听到了。
if (android.os.Build.VERSION.SDK_INT >= 15) {
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);
} else {
// 以下适用于Android2.3及2.3以上的版本上 ,但测试发现4.1系统上不管用。
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");
context.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(Intent.EXTRA_KEY_EVENT, localKeyEvent1);
context.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(Intent.EXTRA_KEY_EVENT, localKeyEvent2);
context.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");
context.sendOrderedBroadcast(localIntent4,
"android.permission.CALL_PRIVILEGED");
}
} 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);
}
}
public void onStart() {
super.onStart();
}
public void onPause() {
super.onPause();
finish();
}
}
3、ITelephony.aidl
package com.android.internal.telephony;
interface ITelephony {
boolean endCall();
void answerRingingCall();
}