3

我想阻止某个电话号码给我打电话。我对所有内容进行了编码,当我将数字放入应用程序时它工作正常。但我不知道如何在安装应用程序后手动输入电话号码的自定义列表。我想更改任何数字并保存它。在PhoneCallStateListener我创建了一个新的类型类Resource.java,我在其中提取了数字。但它没有用。请帮忙。

这是我的代码:

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

public class PhoneCallStateListener extends PhoneStateListener {
    private Context context;

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

    @SuppressWarnings("unchecked")
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
            AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            //Turn ON the mute
            audioManager.setStreamMute(AudioManager.STREAM_RING, true);
            //i want to getnum from here
            Resource res=new Resource();
            String get=res.et.getText().toString();
            //end of getting num
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            try {
                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
                if (incomingNumber.equalsIgnoreCase(get)) {
                    telephonyService.silenceRinger();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            //Turn OFF the mute
            audioManager.setStreamMute(AudioManager.STREAM_RING, false);
            break;
        }
        super.onCallStateChanged(state, incomingNumber);
    }
}

资源.java 文件:

 public class Resource extends Activity {

    Button btsave,btcancel;
    EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.numbers);
        et=(EditText)findViewById(R.id.editNumber);
        btsave=(Button)findViewById(R.id.buttonSave);
        btcancel=(Button)findViewById(R.id.buttonCancel);
        edittext();

    }

    public void edittext() {
        // TODO Auto-generated method stub
        InputStreamReader is=new InputStreamReader(this.getResources().openRawResource(R.raw.numtext));
        BufferedReader buff=new BufferedReader(is);
        StringBuilder finaltext=new StringBuilder();
        String line = null;
        try{
            while((line= buff.readLine())!=null)
            {
                finaltext.append(line);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        et.setText(finaltext);
    }
}
4

0 回答 0