0

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

请任何人都可以查看我的代码并指出我做错了什么?尤其是拒绝功能。

我试图实施这个几天,但它只是不工作!

我有以下代码:

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();
    }
}
}
4

1 回答 1

1

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

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

应该

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

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

注意:确保incomingNumber不是null.

于 2013-04-19T19:03:44.570 回答