将字符串从服务

时间:2016-01-12 19:27:15

标签: android string listener

我目前有一个服务。此服务(活动时)启动PhoneCallListener(由我覆盖),然后基本上收听电话。 在我访问我的服务之前,我有一个String,我将其传递给我的服务。 这一切都很好,但我怎么能把我拥有的字符串传递给我的听众呢?我无法创建任何上下文或任何内容。我的代码:

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
        this.isActive = true;
        this.phoneNum = intent.getStringExtra("num");
        Toast.makeText(this, phoneNum, Toast.LENGTH_LONG).show();
        otherFunc();
        return mStartMode;
    }
    private void otherFunc() {
        PhoneCallListener phoneCallListener = new PhoneCallListener();
        TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(phoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);
    }




class PhoneCallListener extends PhoneStateListener
{
    private boolean isPhoneCalling = false;
    String phoneNumToIgnore = "";

    @Override
    public void onCallStateChanged(int state, String incomingNumber)
    {
        if (TelephonyManager.CALL_STATE_RINGING == state) {
            isPhoneCalling = true;

            /*THIS IS WHERE I WANT THE EXTRA STRING*/

        }
    }

}

1 个答案:

答案 0 :(得分:0)

如果我理解正确,看起来你正试图将电话号码从onStartCommand()传递给忽略听众。为什么不将该数字传递给otherFunc(字符串num),然后在Phonelistener上创建一个构造函数来用它初始化手机监听器?