gpt4 book ai didi

java - 安卓自动拒接来电

转载 作者:行者123 更新时间:2023-12-01 14:39:36 24 4
gpt4 key购买 nike

在我的应用程序中,如果手机处于充电状态,我想拒绝来电。

请任何人检查我的代码并指出我做错了什么?特别是拒绝功能。

我已经尝试实现这个好几天了,但它不起作用!

我有以下代码:

MyPhoneStateListener.java:

import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.BatteryManager;
import android.telephony.PhoneStateListener;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class MyPhoneStateListener extends PhoneStateListener {
private Context contextMember;
private String strRegisterNumber1 = "";
private String strRegisterNumber2 = "";
public FileManager fileManager;
// private ITelephony telephonyService;

public MyPhoneStateListener(Context context) {
super();
// TODO Auto-generated constructor stub
contextMember = context;
}

public void onCallStateChanged(int state, String incomingNumber)
{
fileManager = new FileManager(contextMember);

strRegisterNumber1 = fileManager.getItems(0);
strRegisterNumber2 = fileManager.getItems(1);

switch (state)
{
case TelephonyManager.CALL_STATE_IDLE:
Log.i("DEBUG", "idle");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
AudioManager audioManager = (AudioManager) contextMember.getSystemService(Context.AUDIO_SERVICE);
audioManager.setSpeakerphoneOn(true);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
break;
case TelephonyManager.CALL_STATE_RINGING:
if (incomingNumber == strRegisterNumber1 || incomingNumber == strRegisterNumber2) // register phone caller
{
if (isCharging())
{
reject();
sendSMS(incomingNumber);
} else
{
return;
}
} else
{
reject();
}
break;
}
}

public void reject() // phone call reject
{
TelephonyManager telephony = (TelephonyManager)contextMember.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.silenceRinger();
telephonyService.endCall();

AlertDialog.Builder builder = new AlertDialog.Builder(contextMember);
builder.setTitle("reject status");
builder.setMessage("reject sucess").setCancelable(false).setPositiveButton("OK", null);
AlertDialog alert = builder.create();
alert.show();

} catch(Exception e)
{
e.printStackTrace();
}
}

public boolean isCharging()
{
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = contextMember.registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean bCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
return bCharging;
}

public void sendSMS(String phoneNumber)
{
String message = "I am at home";

try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
Toast.makeText(contextMember.getApplicationContext(), "SMS Sent", Toast.LENGTH_LONG).show();
} catch (Exception e)
{
Toast.makeText(contextMember.getApplicationContext(), "SMS faild, please try again later!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}

最佳答案

我在这里看到的一个问题是:

 if (incomingNumber == strRegisterNumber1 || incomingNumber == strRegisterNumber2)

应该是

 if (incomingNumber.equals( strRegisterNumber1) || incomingNumber.equals( strRegisterNumber2))

字符串比较应该使用equals()而不是==

注意:确保 incomingNumber 不为 null

关于java - 安卓自动拒接来电,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16111753/

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