前のブログでは、https://blog.csdn.net/huangbaokang/article/details/112334420
1.サービスを手動で呼び出して、ブラックリスト番号を傍受します
AIDLを使用して手動で電話を切る方法と、ボタンをクリックして電話を切る機能をトリガーする方法について説明しました。次に、自動的に通話を切断し、ブラックリストに登録された通話の傍受に到達する機能を実現します。
具体的な原則は、ブロードキャストレシーバーを介して通話を切断するサービスを開始し、登録してブロードキャストを開始することです。ハングアップサービスを手動で開始するには、2つの手順があります。ここではブロードキャストは使用されません。これについては後で説明します。
レイアウト
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/btn_main_start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动来电监听"
android:onClick="startListenCall" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="停止来电监听"
android:onClick="stopListenCall"
android:layout_below="@id/btn_main_start"
/>
</RelativeLayout>
Activityクラスの処理は非常に簡単で、サービスを直接開始し、サービスを中断します
package com.hbk.service;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startListenCall(View v) {
startService(new Intent(this, ListenCallService.class));
}
public void stopListenCall(View v) {
stopService(new Intent(this, ListenCallService.class));
}
}
ListenCallServiceクラスを定義し、PhoneStateListenerを使用して、呼び出し状態の処理に重点を置いて、さまざまな呼び出しの状態を監視します。
package com.hbk.service;
import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class ListenCallService extends Service {
private TelephonyManager tm;
private PhoneStateListener listener = new PhoneStateListener() {
// 当通话状态发生改变时调用
/**
* Callback invoked when device call state changes.
*
* @see TelephonyManager#CALL_STATE_IDLE
* @see TelephonyManager#CALL_STATE_RINGING
* @see TelephonyManager#CALL_STATE_OFFHOOK
*/
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:// 空闲 (挂断电话/未来电之前)
Log.e("TAG", "空闲 (挂断电话/未来电之前)");
break;
case TelephonyManager.CALL_STATE_RINGING:// 响铃
Log.e("TAG", "响铃");
// 如果来电电话是黑名单号(110), 就挂断电话
if ("110".equals(incomingNumber)) {
try {
endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:// 接通
Log.e("TAG", "接通");
break;
default:
break;
}
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
/**
* 挂断电话
* @throws Exception
*/
private void endCall() throws Exception {
// 通过反射调用隐藏的API
// 得到隐藏类的Class对象
Class c = Class.forName("android.os.ServiceManager");
// 得到方法所对应的Method对象
Method method = c.getMethod("getService", String.class);
// 调用方法
IBinder iBinder = (IBinder) method.invoke(null,
Context.TELEPHONY_SERVICE);
// 得到接口对象
ITelephony telephony = ITelephony.Stub.asInterface(iBinder);
// 结束通话
telephony.endCall();
}
@Override
public void onCreate() {
super.onCreate();
Log.e("TAG", "Service onCreate()");
// 得到电话管理器
tm = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
// 监听电话状态
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("TAG", "Service onDestroy()");
// 停止电话监听
tm.listen(listener, PhoneStateListener.LISTEN_NONE);
}
}
上記のファイルは、ListenCallServiceで参照する前に、AIDLファイルに基づいてコードを生成する必要があります。
次の2つの権限がマニフェストファイルに追加されます
<!-- 挂断电话 -->
<uses-permission android:name="android.permission.CALL_PHONE"/>
<!-- 读取电话状态 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
そして、サービスを登録します
<service android:name="com.hbk.service.ListenCallService"></service>
クリックをテストして監視を開始し、エミュレータで110に電話をかけると、着信がないことがわかりましたが、レコードは通話ログに表示されます。
数日前に、わかりやすくユーモラスな巨大な人工知能学習ウェブサイトを発見しました。みんなと共有せざるを得ません。クリックしてチュートリアルにジャンプします
2.ブロードキャストを使用して、ブラックリストに登録された番号のSMSを傍受します
ブラックリストに載っている番号の通話やテキストメッセージの傍受を実現するために、ブロードキャストの使用を分析してみましょう。
新しいBootReceiverを作成して、起動後にブロードキャストされるレシーバーを受信します
package com.hbk.service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* 接收开机完成广播的receiver
*
*/
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//启动电话监听的service
context.startService(new Intent(context, ListenCallService.class));
}
}
そして、マニフェストファイルに次の権限を追加します
<!-- 接收开机完成广播的权限 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
放送を登録してください。アクション名は固定文です。覚えておいてください。
<receiver android:name="com.hbk.service.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
仕事で一般的に使用されるブロードキャスト
同様に、ブラックリストに登録されたテキストメッセージを傍受する受信機を定義します
package com.hbk.service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
/**
* 接收来了短信的广播的receiver
*
*/
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//1. 得到intent短信数据, 并封装为短信对象smsMessage
Bundle extras = intent.getExtras();
Object[] pdus = (Object[])extras.get("pdus");
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[])pdus[0]);
//2. 取号码
String number = smsMessage.getOriginatingAddress();
String content = smsMessage.getMessageBody();
Log.e("TAG", number +" : "+content);
//3. 判断是否是黑名单号
if("110".equals(number)) {
//4. 如果是, 中断广播(拦截短信)
abortBroadcast();
Log.e("TAG", "拦截到一个黑名单短信");
}
}
}
マニフェストファイルで構成する
<receiver android:name="com.hbk.service.SmsReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
SmsMessageオブジェクトを使用して、電話番号とテキストメッセージの内容を取得します。テストするには、最初にアプリケーションを展開し、次に仮想マシンをオフにしてテストを再開します。オンにすると、ブラックリストに記載された番号のブロードキャストレシーバーが有効になっていることがわかります。