gpt4 book ai didi

android - android应用开发中如何屏蔽手机号码的来电和短信接收?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:33:28 26 4
gpt4 key购买 nike

我想实现一个应用程序来阻止手机号码接收或发送电话和消息。在我的应用程序中,我在 EditText 框中输入手机号码,然后单击一个按钮来阻止用户输入的手机号码。

我实现了一个 Activity 类如下:

public class BlockNumberActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

((Button)findViewById(R.id.block)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String mobileNumer = ((EditText)findViewById(R.id.mobileNum)).getText().toString();
//How to block entered mobileNumber
}
});

((Button)findViewById(R.id.unblock)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String mobileNumer = ((EditText)findViewById(R.id.mobileNum)).getText().toString();
//How to unblock entered mobileNumber
}
});
}
}

我想我们可以使用BroadcastReceiver。但是我对此没有更多的了解。请给我一个想法如何实现阻止或取消阻止手机号码。请任何人帮助我.....

最佳答案

创建PhoneCallReceiver .java

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;
public class PhoneCallReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneCallStateListener customPhoneListener = new PhoneCallStateListener(context);
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);



}}

现在创建 PhoneCallStateListener .java

import java.lang.reflect.Method;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import com.android.internal.telephony.ITelephony;

public class PhoneCallStateListener extends PhoneStateListener {

private Context context;
public PhoneCallStateListener(Context context){
this.context = context;
}


@Override
public void onCallStateChanged(int state, String incomingNumber) {
SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context);

switch (state) {

case TelephonyManager.CALL_STATE_RINGING:

String block_number = prefs.getString("block_number", null);
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
//Turn ON the mute
audioManager.setStreamMute(AudioManager.STREAM_RING, true);
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Toast.makeText(context, "in"+block_number, Toast.LENGTH_LONG).show();
Class clazz = Class.forName(telephonyManager.getClass().getName());
Method method = clazz.getDeclaredMethod("getITelephony");
method.setAccessible(true);
ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager);
//Checking incoming call number
System.out.println("Call "+block_number);

if (incomingNumber.equalsIgnoreCase("+91"+block_number)) {
//telephonyService.silenceRinger();//Security exception problem
telephonyService = (ITelephony) method.invoke(telephonyManager);
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);
}}

现在在 src 中创建这个 package com.android.internal.telephony 现在在这个包中右键单击 -> New -> File now give name ITelephony.aidl 并粘贴此代码

package com.android.internal.telephony; 

interface ITelephony {

boolean endCall();

void answerRingingCall();

void silenceRinger();
}

注意:代码已在 Android 2.2 (Froyo)、2.3 (GingerBread) 中测试

关于android - android应用开发中如何屏蔽手机号码的来电和短信接收?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9904426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com