我已将ITelephony.aidl
文件添加到src文件夹中,包名称为com.android.internal.telephony包。以下代码为PhoneCallStateListener.java
package com.broad.sowmy.blockingcall;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
/**
* Created by sowmy on 24-10-2016.
*/
public class PhoneCallStateListener extends PhoneStateListener {
private Context context;
String block_num;
SharedPreferences shared;
public PhoneCallStateListener(Context context){
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
shared=context.getSharedPreferences("get",Context.MODE_PRIVATE);
block_num=shared.getString("number",null);
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_RING, true);
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Toast.makeText(context, block_num, Toast.LENGTH_SHORT).show();
Class clazz = Class.forName(tm.getClass().getName());
Method method = clazz.getDeclaredMethod("getITelephony");
method.setAccessible(true);
ITelephony telephonyService = (ITelephony) method.invoke(tm);
System.out.println("Call " + block_num);
if (incomingNumber.equalsIgnoreCase("+91" + block_num)) {
telephonyService = (ITelephony) method.invoke(tm);
telephonyService.silenceRinger();
System.out.println(" in " + block_number);
telephonyService.endCall();
}
} catch (Exception e) {
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
//Turn OFF the mute
audioManager.setStreamMute(AudioManager.STREAM_RING, false);
break;
case PhoneStateListener.LISTEN_CALL_STATE:
}
super.onCallStateChanged(state, incomingNumber);
}
}
PhoneCallReceiver.java位于
之下 package com.broad.sowmy.blockingcall;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
/**
* Created by sowmy on 24-10-2016.
*/
public class PhoneCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager tm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneCallStateListener phoneCallStateListener=new PhoneCallStateListener(context);
tm.listen(phoneCallStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
Mainfest文件如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broad.sowmy.blockingcall">
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"></uses-permission>
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".PhoneCallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
</application>
</manifest>
在ITelephony.aidl
package com.android.internal.telephony;
public interface ITelephony {
boolean endCall();
void answerRingingCall();
void silenceRinger();
}
答案 0 :(得分:0)
在com/android/internal/telephony
文件夹中创建src
文件夹结构,并将ITelephony.aidl
文件放在电话文件夹中。