将字符串从服务传递给侦听器
Pass a string to a Listener from a Service
我目前有一项服务。此服务(激活时)启动 PhoneCallListener(由我覆盖),然后基本上监听 phone 呼叫。
在我访问我的服务之前,我有一个传递给我的服务的字符串。
这一切都很好,但是我怎样才能将我拥有的字符串传递给我的听众呢?我无法创建任何上下文或任何东西。我的代码:
@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*/
}
}
}
如果我理解正确的话,看起来您正试图将 phone 数字传递给 onStartCommand() 中的侦听器以忽略。为什么不将该数字传递给 otherFunc(string num),然后在 Phonelistener 上创建构造函数以用它初始化 phone 侦听器?
我目前有一项服务。此服务(激活时)启动 PhoneCallListener(由我覆盖),然后基本上监听 phone 呼叫。 在我访问我的服务之前,我有一个传递给我的服务的字符串。 这一切都很好,但是我怎样才能将我拥有的字符串传递给我的听众呢?我无法创建任何上下文或任何东西。我的代码:
@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*/
}
}
}
如果我理解正确的话,看起来您正试图将 phone 数字传递给 onStartCommand() 中的侦听器以忽略。为什么不将该数字传递给 otherFunc(string num),然后在 Phonelistener 上创建构造函数以用它初始化 phone 侦听器?